Skip to main content
gamedev.net
🔒 Locked

Var/Class init performance question

Started by lordikon Jun 4, 2009 at 9:40 AM 3 replies 550+ views
Original Post
lordikon
lordikon
Which is faster? 1.) String myStr = "Foo"; or 2.) String myStr("Foo"); Does the first method call the default constructor for String, and then the operator= operator? The second method I'm assuming just calls a copy constructor?
Bregma
Bregma
What language?

In C++ the two generate identical code.
Stephen M. Webb
Professional Free Software Developer
lordikon
lordikon
Yes I meant C++, sorry about the lack of info.

Nice that they generate identical code. What about this?

3.)
String myStr;
myStr = "Foo";

Obviously this is a poor choice anyway because it could be done on one line, and it leaves 'myStr' uninitialized when it is created, which is bad.
SiCrane
SiCrane
That will default construct myStr and then call the assignment operator. This will generally be slower than direct initialization.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.