admin管理员组文章数量:1326314
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 mon 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 mon answer. I understand that Javascript is an interpreter language while C++ is piler 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 me 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.
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 mon 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 mon answer. I understand that Javascript is an interpreter language while C++ is piler 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 me 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.
Share Improve this question edited Oct 7, 2012 at 4:35 Assad Ebrahim 6,3618 gold badges44 silver badges68 bronze badges asked Mar 18, 2011 at 12:27 Diff.ThinkrDiff.Thinkr 8651 gold badge8 silver badges16 bronze badges 7- 1 -1 You cannot ask on a construct in two such unrelated languages. As a matter of fact, the meaning of the same code snippet (filling the blanks so that it actually piles) might be pletely different! – David Rodríguez - dribeas Commented Mar 18, 2011 at 13:24
- -1 For the same reasons as David - your code probably isn't valid C++ (unless you've #defined var) and if you did correct the first code sample it probably wouldn't have the same effect as the first. – JoeG Commented Mar 18, 2011 at 13:57
-
-1 In C++, assuming some reasonable definition of
var
, the two code snippets have different semantics. It doesn't matter which is faster -- they do different things. – Robᵩ Commented Mar 18, 2011 at 15:04 - @David Rodríguez - dribeas @Joe Gauterin @Rob Adams I disagree. I think he should have asked for even more languages. Why not talk about the theory of it? And I don't think anyone suggested this was meant to be valid C++. JavaScript or just pseudocode is ok too. There's no need to write out the same bit of code over and over in every language you know just to ask a question on SO. – Buh Buh Commented Mar 18, 2011 at 17:04
-
@Buh Buh: The problem is that the code in question is not patible with both languages, and the answer is very dependent on the language. I don't know Javascript, in Java where everything are references they are the same, in C# it depends on whether the object is a struct or class, in C++ it depends on the definition of
var
(that in c++ must be a declared type, but it could have a hidden reference, so it is still unanswerable... – David Rodríguez - dribeas Commented Mar 18, 2011 at 22:13
7 Answers
Reset to default 7In 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 piler 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 pile (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 piler 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.
本文标签: Which fragment will execute fastergenerate fewer lines of code(CJavaScript)Stack Overflow
版权声明:本文标题:Which fragment will execute fastergenerate fewer lines of code?(C++JavaScript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742202499a2432222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论