-
January 6th, 2009, 04:12 AM
#1
[RESOLVED] Faster to construct many times or construct once and assign many times?
Hey.
I have a for loop. Inside that for loop I pass a point to a function. The point is made from two integer values that are part of (declared and initialised inside) the for loop. I have the option to either create a point before the loop and then assign its x and y values manually each iteration:
Code:
point.x = x;
point.y = y;
some_func(point);
or construct a point each iteration from the two values:
Code:
some_func(point(x, y));
Premature optimisation aside, this is a question that I have asked myself many times before and I think it's time I chose the right option instead of wasting energy each time it happens pondering which is faster.
Thanks!
-
January 6th, 2009, 04:40 AM
#2
Re: Faster to construct many times or construct once and assign many times?
The only way you're going to get a definitive answer is to put together a test harness and time the two approaches. Never try to second-guess such fine differences.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 6th, 2009, 04:48 AM
#3
Re: Faster to construct many times or construct once and assign many times?
The best thing to do is test it, since the answer to your question is compiler and system dependent. However, that said, on tests that I had done in the past with VS2005 on a P4, char's where certainly faster to construct than assign, and I think the same was also true for int's. I'm not sure about short's (on some systems, despite being two bytes, these are slower than int's). Doubles were faster to assign than construct.
Again, I stress, that testing is the best option since the answer to your question is compiler and system dependent. But, my general rule (which may be misplaced) is, built in types of 4 bytes or less where the value is different on each pass, I'll construct and initialise inside the loop, but anything else (including user defined types), I'll construct and initialise outside of the loop, and assign within the loop.
If you are really bothered about which is faster and want an absolute answer though, then profile it, and pick the most appropriate solution.
I assume that you are passing point to some_func by reference?
Last edited by PredicateNormative; January 6th, 2009 at 04:52 AM.
-
January 6th, 2009, 07:08 AM
#4
Re: Faster to construct many times or construct once and assign many times?
Originally Posted by Graham
The only way you're going to get a definitive answer is to put together a test harness and time the two approaches. Never try to second-guess such fine differences.
Originally Posted by PredicateNormative
The best thing to do is test it, since the answer to your question is compiler and system dependent. However, that said, on tests that I had done in the past with VS2005 on a P4, char's where certainly faster to construct than assign, and I think the same was also true for int's. I'm not sure about short's (on some systems, despite being two bytes, these are slower than int's). Doubles were faster to assign than construct.
Again, I stress, that testing is the best option since the answer to your question is compiler and system dependent. But, my general rule (which may be misplaced) is, built in types of 4 bytes or less where the value is different on each pass, I'll construct and initialise inside the loop, but anything else (including user defined types), I'll construct and initialise outside of the loop, and assign within the loop.
If you are really bothered about which is faster and want an absolute answer though, then profile it, and pick the most appropriate solution.
I assume that you are passing point to some_func by reference?
Ok. Seems I've found my answer. Are there any profilers that you'd recommend that work with Visual Studio? I'd be better off using a proper one than writing my own test code as at least I can use it again.
In the actual scenario, yes, I do pass point by reference to some_func.
-
January 6th, 2009, 07:41 AM
#5
Re: Faster to construct many times or construct once and assign many times?
I don't know of a free one, but the one I used in my old company to do the tests was DevPartner by Compuware. You can get a free trial version...
http://www.compuware.com/products/de...rk=200809-5755
-
January 6th, 2009, 08:12 AM
#6
Re: Faster to construct many times or construct once and assign many times?
Originally Posted by PredicateNormative
Cheers.
-
January 6th, 2009, 08:38 AM
#7
Re: [RESOLVED] Faster to construct many times or construct once and assign many times
i think its safe to assume that assignment is in most cases faster.
especially for classes that have dynamic allocation, strings, arrays etc.
unless it is a small POD.
-
January 6th, 2009, 09:04 AM
#8
Re: [RESOLVED] Faster to construct many times or construct once and assign many times
This brings to mind Item #18 of Sutter and Alexandrescu's C++ Coding Standards: declare variables as locally as possible, but with an exception that it can sometimes be beneficial to hoist a variable out of a loop. However, their explanation of the exception is that "most of the time that won't obfuscate the code's intent at all, and it can actually help clarify what work is done inside the loop and what calculations are loop-invariant". That explanation does not seem to apply here since the work is indeed done inside the loop, hence "hoisting" the variable is just a matter of optimisation.
-
January 6th, 2009, 09:19 AM
#9
Re: [RESOLVED] Faster to construct many times or construct once and assign many times
It REALL is a case for measurement (or at least careful analysis) and has no "correct" answer i the general case.
I have even found cases where:
Code:
Class basicInstance;
// Lots of setup for basicInstance
for (....)
{
Class loopInstance = basicInstance;
// Tweak this from the standard
....
}
is the fastest.
I agree with the premis of let "intent" be your initial guiding factor, and only deviate from that if you MEASURE a performance issue (you know my whole speech on that )
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 6th, 2009, 01:13 PM
#10
Re: [RESOLVED] Faster to construct many times or construct once and assign many times
[QUOTE=Mybowlcut;1798716]
Originally Posted by Mybowlcut
Premature optimisation aside,
The best alternative is,
some_func(point(x, y));
It expresses your intent in the shortest and most restrictive way. It provides the compiler with more information about your intent than any other alternative and it thereby creates the most room for optimizations. This should guide your choise, not system dependent measurements.
If exactly this spot turns out to be a performance bottleneck then chances are that none of these choises will give the fastest code anyway. You most likely will do a rewrite of a larger section of code.
Last edited by _uj; January 6th, 2009 at 02:02 PM.
-
January 6th, 2009, 01:56 PM
#11
Re: Faster to construct many times or construct once and assign many times?
Originally Posted by Mybowlcut
Ok. Seems I've found my answer. Are there any profilers ........
As I've argued in my previous post. Don't make a choise in cases like this based on system dependent measurements.
-
January 6th, 2009, 02:07 PM
#12
Re: [RESOLVED] Faster to construct many times or construct once and assign many times
Originally Posted by _uj
The best alternative is,
some_func(point(x, y));
It expresses your intent in the shortest and most restrictive way.
I agree. In fact, I would not call it an alternative, but rather the first thing that should be reached for since it follows the principle of declaring variables near first use.
Originally Posted by _uj
This should guide your choise, not system dependent measurements.
How would you find the performance bottle neck without measurement?
Originally Posted by _uj
If exactly this spot turns out to be a performance bottleneck then chances are that none of these choises will give the fastest code anyway. You most likely will do a rewrite of a larger section of code.
Wouldn't that depend on the (relative) cost of construction and assignment? It does seem reasonable to me that "hoisting" the variable out of the loop such that assignment is used instead of construction could be a valid optimisation.
-
January 6th, 2009, 02:12 PM
#13
Re: [RESOLVED] Faster to construct many times or construct once and assign many times
Originally Posted by _uj
The best alternative is,
some_func(point(x, y));
in this case yes, its a POD
if there is dynamic allocation, then this might not be prefered.
-
January 7th, 2009, 11:15 AM
#14
Re: [RESOLVED] Faster to construct many times or construct once and assign many times
Don't forget that the optimiser may well hoist the declaration out of the loop for you (and in a more efficient way than you could achieve manually), so the point could well be moot.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 7th, 2009, 06:19 PM
#15
Re: [RESOLVED] Faster to construct many times or construct once and assign many times
Originally Posted by Graham
Don't forget that the optimiser may well hoist the declaration out of the loop for you (and in a more efficient way than you could achieve manually), so the point could well be moot.
Are you serious? Are they really that intelligent?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|