BigB23USAABC All American 913 Posts user info edit post |
In C,
Suppose you want to declare a variable, and say you need some initial numberic value in it, and then you are going to use that variable later, like:
int var1; var1=0; var1=var1+7;
Would it be more efficient, as far as the final compiled program goes to say:
int var1=0; var1=var1+7;
or would the machine instructions for those two versions be initially the same?8/30/2005 6:03:00 PM |
OmarBadu zidik 25071 Posts user info edit post |
for a good compiler - same 8/30/2005 6:09:23 PM |
moron All American 34142 Posts user info edit post |
I don't know for sure, but I would guess they compile to the same thing. 8/30/2005 6:09:36 PM |
Shaggy All American 17820 Posts user info edit post |
Write your code so it is as readable as possible, document well, and get a good compiler. 8/30/2005 6:11:00 PM |
Lowjack All American 10491 Posts user info edit post |
trying to make optimizations like these are idiotic unless your time is not very valuable. Most of your time should be focused on design and algorithmic optimizations. 8/30/2005 8:01:42 PM |
Excoriator Suspended 10214 Posts user info edit post |
yea, this question is only relevant if you're writing assembly - and even then, i'd hope the assembler would assemble it the same either way. 8/30/2005 8:15:05 PM |
JaegerNCSU Veteran 245 Posts user info edit post |
Both methods will reduce to something like this (probably exactly like this, at least for msvc++):
mov dword ptr [var1],0 mov eax,dword ptr [var1] add eax,7 mov dword ptr [var1],eax
^^ While that is probably true for plain old C, in C++ you have to be very careful with default versus copy constructors. For example, if you have a class that is expensive to construct but lightweight in terms of copy construction and assignment, then tracking down the places where you do:
someClass obj; obj = obj2;
and changing it to:
someClass obj = obj2;
could potentially give you huge performance wins. 8/30/2005 8:21:13 PM |
darkone (\/) (;,,,;) (\/) 11610 Posts user info edit post |
FORTRAN bitches 8/30/2005 11:06:08 PM |
smoothcrim Universal Magnetic! 18966 Posts user info edit post |
int var1=0; var1+=7;
[Edited on August 31, 2005 at 12:00 AM. Reason : the important stuff has been said, that's just how I'd write it] 8/30/2005 11:59:19 PM |
Excoriator Suspended 10214 Posts user info edit post |
int var1 = 7;
damn 8/31/2005 10:49:23 AM |
ZeroDegrez All American 3897 Posts user info edit post |
#define var1 7
I win. 8/31/2005 12:56:15 PM |
Excoriator Suspended 10214 Posts user info edit post |
^ he wins 8/31/2005 2:42:52 PM |
InsaneMan All American 22802 Posts user info edit post |
7; i win 8/31/2005 10:52:18 PM |