By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,669 Members | 2,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,669 IT Pros & Developers. It's quick & easy.

for(int i... and speed

P: n/a
Is there any difference in speed between

for(int i=0; i<I; i++){
for(int j=0; j<J; j++){
//something
}
}

and

int i, j;
for(i=0; i<I; i++){
for(j=0; j<J; j++){
//something
}
}

?

Aug 4 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Misiowaty wrote:
Is there any difference in speed between

for(int i=0; i<I; i++){
for(int j=0; j<J; j++){
//something
}
}

and

int i, j;
for(i=0; i<I; i++){
for(j=0; j<J; j++){
//something
}
}

?


Usually, no. In an unlikely event there is a difference, it is so
minuscule that you shouldn't even worry your head about it.

The much bigger difference is that the latter variation introduces 'i' and
'j' into the surrounding scope whereas the former variation doesn't.

V
Aug 4 '05 #2

P: n/a
I do not believe that it will make much difference, but I think that
the second option is faster than the first one (milliseconds) therefore
theoretically, in the first example the compiler will go to create and
to destroy variable j I times.

Aug 4 '05 #3

P: n/a
MarcusRescue wrote:
I do not believe that it will make much difference, but I think that
the second option is faster than the first one (milliseconds) therefore
theoretically, in the first example the compiler will go to create and
to destroy variable j I times.


Depending on the sizes of I and J it will probably unroll the loop
several times. If they are small enough, they will be allocated very few
times.
Since this particular instance is such a miniscule if any increase in
speed, you should go with clearer code rather than boosting speed.
--Paul
Aug 4 '05 #4

P: n/a
there's no such thing as destruction of int's. Basicly, there will be
two ints in memory (in registers, or whatever) and they will be reused.
I think most compilers will produce identical code for the two cases if
i and j aren't used outside of the loop.

and I think there's no speed difference either in the two cases. If you
need to know what's the difference in speed, why don't you measure it
yourself...

Aug 4 '05 #5

P: n/a

Paul Bilnoski schreef:
MarcusRescue wrote:
I do not believe that it will make much difference, but I think that
the second option is faster than the first one (milliseconds) therefore
theoretically, in the first example the compiler will go to create and
to destroy variable j I times.


Depending on the sizes of I and J it will probably unroll the loop
several times. If they are small enough, they will be allocated very few
times.


In fact, in modern compilers and modern CPU's I and J will be put in
registers (for both cases). That means they won't be allocated at all.

HTH,
Michiel Salters

Aug 5 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.