473,386 Members | 1,694 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,386 software developers and data experts.

++i is faster (or) i++ is faster in a for loop???

In a for loop like for(i=0;i<=10;i++) and for(i=0;i<=10;++i).... i have heard saying that writing ++i in the for loop is faster rather than writing i++.... is that true??... is there any explaination for that???
Oct 13 '14 #1

✓ answered by Banfa

The question is not "++i is faster (or) i++ is faster in a for loop" but "++i is or i++ faster", i.e. being in a for loop is irrelevant to the speed of the operation.

Taking the 2 operators then what they do is
  1. ++i - increment the value of i and return the new value
  2. i++ - increment the value of i and return the original value

In 1 having incremented the value of i you can then just use the value of i, however in i++ having incremented the value of i you have to return the original value so you have to deal with 2 values at the same time, the original and new value of i represented by the pseudo code

Expand|Select|Wrap|Line Numbers
  1. ++i
  2. {
  3.     INCREMENT i
  4.     RETURN i
  5. }
  6.  
  7. i++
  8. {
  9.     result = i
  10.     INCREMENT i
  11.     RETURN result
  12. }
  13.  
You can see an extra variable is created and copied to so conceptually ++i is faster.

However we have yet to talk about type because this only really comes into play for classes that have defined operator++() and operator++(int). In the 2 for loops you give if i is an int (or any other POD type) then the compiler/optimiser is clever enough to see that you don't use the return value at all and optimise away the code that does the copy operation for the i++ implementation. However a classes implementation of the operators is always going to have to do a copy to correctly implement i++.

So the real answer is that ++i is faster than i++ for a class (that supports those operations) but whether it makes a difference to you run time speed is going to be rather dependent on the complexity of the class, e.g. for a simple class holding a single int you will see little difference between ++i and i++ for a complex class a lot of data (or component classes) you might see some difference, especially in a for loop that is repeatedly doing the unrequired copies.

As a matter of course I personally always use ++i in for loops for iterators.

2 1547
Banfa
9,065 Expert Mod 8TB
The question is not "++i is faster (or) i++ is faster in a for loop" but "++i is or i++ faster", i.e. being in a for loop is irrelevant to the speed of the operation.

Taking the 2 operators then what they do is
  1. ++i - increment the value of i and return the new value
  2. i++ - increment the value of i and return the original value

In 1 having incremented the value of i you can then just use the value of i, however in i++ having incremented the value of i you have to return the original value so you have to deal with 2 values at the same time, the original and new value of i represented by the pseudo code

Expand|Select|Wrap|Line Numbers
  1. ++i
  2. {
  3.     INCREMENT i
  4.     RETURN i
  5. }
  6.  
  7. i++
  8. {
  9.     result = i
  10.     INCREMENT i
  11.     RETURN result
  12. }
  13.  
You can see an extra variable is created and copied to so conceptually ++i is faster.

However we have yet to talk about type because this only really comes into play for classes that have defined operator++() and operator++(int). In the 2 for loops you give if i is an int (or any other POD type) then the compiler/optimiser is clever enough to see that you don't use the return value at all and optimise away the code that does the copy operation for the i++ implementation. However a classes implementation of the operators is always going to have to do a copy to correctly implement i++.

So the real answer is that ++i is faster than i++ for a class (that supports those operations) but whether it makes a difference to you run time speed is going to be rather dependent on the complexity of the class, e.g. for a simple class holding a single int you will see little difference between ++i and i++ for a complex class a lot of data (or component classes) you might see some difference, especially in a for loop that is repeatedly doing the unrequired copies.

As a matter of course I personally always use ++i in for loops for iterators.
Oct 13 '14 #2
Luuk
1,047 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <sys/time.h>
  3.  
  4. int main() {
  5.         struct timeval start, stop;
  6.         int i;
  7.         long long int u1, u2;
  8.  
  9.         gettimeofday(&start, NULL);
  10.         for(i=0;i<=1000000;i++) { }
  11.         gettimeofday(&stop, NULL);
  12.         u1 = stop.tv_usec - start.tv_usec;
  13.         //printf("%lld\n", stop.tv_usec - start.tv_usec);
  14.  
  15.         gettimeofday(&start, NULL);
  16.         for(i=0;i<=1000000;++i) { }
  17.         gettimeofday(&stop, NULL);
  18.         u2 = stop.tv_usec - start.tv_usec;
  19.         //printf("%lld\n", stop.tv_usec - start.tv_usec);
  20.         //printf("\n");
  21.         printf("%lld %lld %lld\n", u1, u2, u1 - u2);
  22. }
  23.  
Expand|Select|Wrap|Line Numbers
  1. luuk@opensuse:~/tmp> for f in {1..10}; do ./test1; done
  2. 4389 4168 221
  3. 3769 3775 -6
  4. 4357 4390 -33
  5. 4408 4386 22
  6. 4410 3794 616
  7. 3762 3849 -87
  8. 4572 4384 188
  9. 4412 4386 26
  10. 4139 3759 380
  11. 3770 4178 -408
  12. luuk@opensuse:~/tmp>
  13.  
so, b......t ;-)
Oct 18 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: phenix151 | last post by:
I am tring to read numbers from the command line. they are to be input like this: 12.34 14.67 82 227 -17 I know how many numbers are to be input by asking. I want to put this in a for or a...
1
by: Kamyk | last post by:
Hello all! I have such question to all of you. I have some tables linked from MS SQL Server 2000. Is time of processing query based on these linked tables from MS SQL Server 2000, faster or...
3
by: Darren Clark | last post by:
Is there a difference in performance betwen these 2 styles of code? As it both does the same thing... if (row.ToString() == "1") this.Visa.Text = "Yes"; else this.Visa.Text = "No"; verses
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
2
by: tinnews | last post by:
I'm trying to minimise the overheads of a small Python utility, I'm not really too fussed about how fast it is but I would like to minimise its loading effect on the system as it could be called...
2
by: Lawrence Krubner | last post by:
Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString",...
7
by: Quidnunc | last post by:
First, I am completely new and learning on my own. I would like the following code to print: "Why doesn't this print?" and "I would like to print the sum of nc: 5". What am I doing wrong. ...
1
by: omar999 | last post by:
I have a working example below of a single file input upload via php and now I'd like to upload multiple files either by iterating through each input type="file". I've read that the multiple...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.