Which fragment will execute faster / generate fewer lines of code? (C++ / JavaScript)
Which of these is more efficient in terms of speed (number of lines of code generated).
var x=obj.mem;
x.do1();
x.do2();
or
obj.mem.do1();
obj.mem.do2();
I just wrote those in a generic pattern. Specifically, if I have to access a member( or a member of a member ) , is it better to assign the common part to a variable and then use that variable or to call it directly as shown in the second case.
I'm concerned mainly about C++ and JavaScript (if it matters). Thank You.
EDIT-> PS.I did not ask a common answer. I understand that Javascript is an interpreter language while C++ is compiler based. The little knowledge I have of JavaScript is self taught and we learned C++ for 2 years at school (and that too Turbo C++) and the rest is again self taught. So, forgive m开发者_Go百科e for the confusion. I was expecting a general result assuming that the constructs are valid and considering any possible variations. Anyway, my major doubts were cleared. Thanks.
Conclusion: JS is faster with local variables (though negligible) and C++ will probably optimise to get almost equal results.
Thank You for all the input.
In JavaScript the first one is faster. You would think that there shouldn't be much difference, but I ran into this while developing Dreamweaver extensions (they use JavaScript) and there is a huge difference.
I advise you to avoid long chains.
Benchmarking:
var x = {y:{z:null}};
var start = (new Date).getTime();
for(var i=0;i<1000000;i++){
x.y.z = i;
}
alert((new Date).getTime()-start);
vs.
var x = {y:{z:null}};
var start = (new Date).getTime();
var q = x.y;
for(var i=0;i<1000000;i++){
q.z = i;
}
alert((new Date).getTime()-start);
The second one is about 10% faster in my Firefox. But keep in mind that this is a minimalist scenario. If you work with larger object and deeper levels the difference will probably go up.
And of course it does:
var x = {a:{b:{c:{d:{y:{z:null}}}}}};
var start = (new Date).getTime();
for(var i=0;i<1000000;i++){
x.a.b.c.d.y.z = i;
}
alert((new Date).getTime()-start);
vs.
var x = {a:{b:{c:{d:{y:{z:null}}}}}};
var start = (new Date).getTime();
var q = x.a.b.c.d.y;
for(var i=0;i<1000000;i++){
q.z = i;
}
alert((new Date).getTime()-start);
Is 30% enough for it not to be negligible?
Regards,
Alin
In C++, with the compiler optimiser turned on, they should both perform identically (at least on average).
At least in JavaScript, by assigning the property to a local variable, you avoid looking up obj
in a potential high (like global) scope, which can be "slow".
The rule of thumb for JS is: If you have to access a non-local object more than once, make it local.
Same for nested properties. Instead of accessing a.b.c.d.x
and a.b.c.d.y
, it is better to assign a.b.c.d
to a variable, if possible.
If you are interested in performance in general, have a look at the book High Performance JavaScript.
Depending in the type of x
, the first form might not even compile (if the type has no copy-constructor).
I would probably go for:
the_type& x = obj.mem;
x.do1();
x.do2();
Note that accessing members from the outside is a very bad idea. It goes against one of the basic principles of object-oriented programming: encapsulation. Here is what you really should be doing:
obj.do_stuff();
Of course, it is hard to give any more advice without knowing what do1
, do2
and mem
actually are.
In C++ any sub-decent compiler will optimize the difference away.
In Javascript the difference should be neglectible. Don't waste your time on stuff like that unless you are building a library to be used by millions like jQuery.
EDIT
Apparently some people don't understand what I mean with neglectible in the argument above. Yes, you can benchmark this stuff and see that it's 30% faster to create local variables. But 1.30 * 0.003
milliseconds is not noticeable, thus neglectible, unless it's executed millions of times, for example in a library.
Local variables in JavaScript are faster because The further into the chain, the slower the resolution.
The difference is absolutely minimal, and neglectable.
精彩评论