|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Hi there,
Tell me which is more efficient of these two FOR LOOPS for(int i=0;i<10;i++); or for(int i=0;i<10;++i); pls:-Justify your Answers. Thanx Devs |
|
#2
|
||||
|
||||
|
Hello and welcome to Devshed.
Please use an informative thread title in future. For example, in this case, something like "which of these for loops is more efficient" would be a good choice. Second, what language are you looking at? I'm guessing this is highly language dependant. Third, premature optimisation is the root of all evil. Something like this is going to provide extremely minor benefits, if any, in most cases. Fourth - have you tried benchmarking it yourself? --Simon
__________________
|
|
#3
|
||||
|
||||
|
There should be no difference in a compiled language.
There should be very little (IF ANY) difference in an interpreted language and it would be impossible to tell which would be more efficient. Let the compiler optimise for you, you have to read the code easily, not the computer. As said above, benchmark it yourself.
__________________
~James [Not currently seeking freelance work] Like philosophy or interested in spirituality? Philosophorum. Game Dev Experts Forums Foresight Linux - Because your desktop should be cool! Linux FAQ FedoraFAQ UbuntuGuide |
|
#4
|
||||
|
||||
|
The ++i will be more efficient, but I won't specify the reason why for the next few days, because this question smells an awful lot like a cut-and-paste homework assignment. I'll also say that when using integer variables, it may not matter for good optimizing compilers because they'll probably optimize it the correct way anyway.
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month |
|
#5
|
||||
|
||||
|
Now that a week has passed since the last post, I'll answer this question now. The answer should be fairly obvious if you work with operator overloading. See the notes to overload ++ and -- in this tutorial
Note that for a post-increment operator, it returns a value instead of a reference Quote:
What this means is that, with ++i, it needs to (1) find the object (i) in memory, (2) increment it and (3) return a reference to that object. In case of i++, it needs to (1) find the object, (2) copy it to a temporary location, (3) increment the original object and (4) return the value from the temporary location. The extra operation is in copying to a temporary location. This doesn't take much effort for an integer, but takes more for a larger class object. |
|
#6
|
|||||
|
|||||
|
Note that in Java (1.5), there is no difference in the bytecode:
Java Code:
__________________
Yawmark class Sig{public static void main(String...args){\u0066or(int \u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray() )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>> +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}} |
|
#7
|
||||
|
||||
|
Same in C#. When the value of the incrementer (-or?) is not used (like in for loops, and other times when the incrementation is the whole statement), then the IL is the same whether you use post- or pre-incrementation.
Code:
// C# int i = 0; i++; // or ++i, doesn't matter // MSIL // (Comments are explanatory, not actually part of MSIL) .locals init ([0] int32 i) IL_0000: ldc.i4.0 // load value 0 IL_0001: stloc.0 // store in location 0 (i) IL_0002: ldloc.0 // load location 0 (i) IL_0003: ldc.i4.1 // load value 1 IL_0004: add IL_0005: stloc.0 // store in location 0 However, when you use the value of i, then things are only slightly different between post- and pre-incrementation. Code:
givens
// C# int a; int i = 2; // MSIL .locals init ([0] int32 a, [1] int32 i) IL_0000: ldc.i4.2 IL_0001: stloc.1 Code:
post-incrementation
// C# a = i++; // a == 2, i == 3 // MSIL IL_0002: ldloc.1 IL_0003: dup IL_0004: ldc.i4.1 IL_0005: add IL_0006: stloc.1 IL_0007: stloc.0 Code:
pre-incrementation
// C# a = ++i; // i == 3, a == 3 // MSIL IL_0002: ldloc.1 IL_0003: ldc.i4.1 IL_0004: add IL_0005: dup IL_0006: stloc.1 IL_0007: stloc.0 No much difference in the IL, just whether dup is used before or after the add. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > which of these for loop styles is more efficient ( was "Plz Help...." ) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|