473,385 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

for(int i... and speed

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
5 1830
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin MOKREJ© | last post by:
Hi, is this a bug or "feature" that I have to use float() to make int() autoconvert from it? $ python Python 2.3.4 (#1, Feb 14 2005, 10:00:27) on linux2 Type "help", "copyright", "credits"...
3
by: Antoine | last post by:
What is considered best when doing an application that allows you to browse data eg by month? I've currently got an app working so it does a complete refresh when you change the month you are...
49
by: utab | last post by:
Why is for(int i=0; i < 100; ++i) poor?
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
8
by: aine_canby | last post by:
>>v = raw_input("Enter: ") Enter: kjjkj Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'kjjkj' In my program I need...
4
by: RS | last post by:
Hi all, I was told that using unsigned int instead of int can speed up the code. Is this true? If so, why? Are there are any other rules one should follow to optimize the code for speed (i.e....
5
by: Andrew Hedges | last post by:
Wherein I attempt to debunk some myths about the relative merits of the two methods for programmatically adding content to a web page: ...
0
by: bernie7242yanez | last post by:
download crack for speed optimizer 2.0 http://cracks.00bp.com F R E E
5
by: pereges | last post by:
I wrote a small program to check the range for int(signed) and long int(signed) on my machine: #include <stdio.h> #include <limits.h> int main(void) { printf("INT_MIN:%d INT_MAX: %d",...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.