Which is faster ... using dynamic or casting?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Which is faster ... using dynamic or casting?

Dinko Pavicic
Hi all,
I'm doing small 2D engine on top of Molehill so naturally I want to do thing as fast as possible.

I have implementation like this:

class Entity extends Node
{
   public var material(default, null):Material;
}

in material property I'm putting objects extended from Material class that have some additional functions/properties from the base Material class.

Is it faster to do casting when getting the actual type extended from Material like this:

var extMat:SpriteMaterial = cast(e.material, SpriteMaterial);
extMat.blendMode = BlendModePreset.ADD;

or to declare material property 'Dynamic' like this:
class Entity extends Node
{
   public var material(default, null):Dynamic;
}
and then I can simply access wanted property like this:
e.material.blendMode = BlendModePreset.ADD;


Many thanks in advance,
D.
Reply | Threaded
Open this post in threaded view
|

Re: Which is faster ... using dynamic or casting?

MarcWeber
1) You should talk about the backend you're using

2) its easy to write your own benchmarks

3) For JS, AS, ... it should not make a huge difference because
  dynamic type checking has to take place anyway because otherwise the
  virtualmachines would crash?
  That's why I'd not expect a huge difference - I may be mistaken
  though.

Marc Weber

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Which is faster ... using dynamic or casting?

Nicolas Cannasse
In reply to this post by Dinko Pavicic
Le 23/09/2011 15:32, Dinko Pavicic a écrit :

> Hi all,
> I'm doing small 2D engine on top of Molehill so naturally I want to do thing
> as fast as possible.
>
> I have implementation like this:
>
> class Entity extends Node
> {
>     public var material(default, null):Material;
> }
>
> in material property I'm putting objects extended from Material class that
> have some additional functions/properties from the base Material class.
>
> Is it faster to do casting when getting the actual type extended from
> Material like this:
>
> var extMat:SpriteMaterial = cast(e.material, SpriteMaterial);
> extMat.blendMode = BlendModePreset.ADD;
>
> or to declare material property 'Dynamic' like this:
> class Entity extends Node
> {
>     public var material(default, null):Dynamic;
> }
> and then I can simply access wanted property like this:
> e.material.blendMode = BlendModePreset.ADD;

casting on Flash9 is always better because it's not THAT slow and after
that you will get faster field access + it's also more typesafe.

Another possibility that I did in physaxe is to add to your Material
class a value for each possible subclass (defined in the constructor
subclass). This is not very nice OO but it works :)

Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Which is faster ... using dynamic or casting?

Dinko Pavicic
In reply to this post by MarcWeber
1) Don't know what you mean under 'backend'?

2) It's easy for flash/swf output since I'm already on it, but wanted to know generally about it(on other targets (cpp [ios, android, desktop], JS).
That would take some time to setup and check (compilers, sdks etc.). I'm sure  people here have some experience with this, so asking a question should save my and probably someone else's time in the future  regarding that matter.

3) I'll check it out for flash and post the results.

D.
Reply | Threaded
Open this post in threaded view
|

Re: Which is faster ... using dynamic or casting?

Dinko Pavicic
In reply to this post by Nicolas Cannasse
Thanks Nicolas that was helpfull!

Not sure what you ment: 'add to your Material class a value for each possible subclass'
but I'll check out the physaxe source to take a deeper look at that approach.

Thanks again,
Dinko
Reply | Threaded
Open this post in threaded view
|

Re: Which is faster ... using dynamic or casting?

luca deltodesco
like:

class Super {
    public var subtype1:SubType1;
    public var subtype2:SubType2;
}

class SubType1 {
    function new() { subtype1 = this; }
}
class SubType2 {
    function new() { subtype2 = this; }
}

then rather than:

if(Std.is(obj,SubType1)) you could do if obj.subtype1==obj, or obj.subtype1!=null
and rather than: cast(obj,SubType1) you can just do obj.subtype1

obviously this doesn't allow for arbitrary children as it has to be hard coded.

> Date: Fri, 23 Sep 2011 09:03:09 -0700

> From: [hidden email]
> To: [hidden email]
> Subject: [haXe] Re: Which is faster ... using dynamic or casting?
>
> Thanks Nicolas that was helpfull!
>
> Not sure what you ment: 'add to your Material class a value for each
> possible subclass'
> but I'll check out the physaxe source to take a deeper look at that
> approach.
>
> Thanks again,
> Dinko
>
> --
> View this message in context: http://haxe.1354130.n2.nabble.com/Which-is-faster-using-dynamic-or-casting-tp6824248p6824764.html
> Sent from the Haxe mailing list archive at Nabble.com.
>
> --
> haXe - an open source web programming language
> http://haxe.org

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Which is faster ... using dynamic or casting?

MarcWeber
In reply to this post by Dinko Pavicic
Excerpts from Dinko Pavicic's message of Fri Sep 23 17:52:41 +0200 2011:
> 1) Don't know what you mean under 'backend'?
JS/PHP/Neko/Flash

> 2) It's easy for flash/swf output since I'm already on it, but wanted to
> know generally about it(on other targets (cpp [ios, android, desktop], JS).
That's the issue: There is no "fastest" general way for all backends.
They all behave differently in some way. I'd say PHPs behaviour is most
different if you use nested lambdas and closures.

> That would take some time to setup and check (compilers, sdks etc.). I'm
> sure  people here have some experience with this, so asking a question
> should save my and probably someone else's time in the future  regarding
> that matter.
No, targets change very fast as well. Implementations also.

Eg if you also want to use JS than it matters a lot whether you have
debugging enabled. Eg Opera let's you switch this on/off.
Luckily the slow IE-7 is disappearing slowly. Using that target all hope
was lost compared to the other JS implementations found in Firefox,
Chrome, ..

In short: Optimize as much as required by your current application only.
Don't even try to optimize something for "future use" or think twice
about it. Requirements usually change too fast ...

Marc Weber

--
haXe - an open source web programming language
http://haxe.org