which of these for loop styles is more efficient ( was "Plz Help...." )
Discuss which of these for loop styles is more efficient ( was "Plz Help...." ) in the Other Programming Languages forum on Dev Shed. which of these for loop styles is more efficient ( was "Plz Help...." ) A place for discussing programming languages not covered in specific forums such as Assembler, COBOL, etc. - you get the idea.
Posts: 10,101
Time spent in forums: 3 Months 3 Weeks 6 h 8 m 34 sec
Reputation Power: 0
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.
Posts: 5,538
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 30 m 28 sec
Reputation Power: 1050
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.
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 36 m 15 sec
Reputation Power: 4080
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
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 36 m 15 sec
Reputation Power: 4080
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:
As to why the function returns Complex instead of Complex &, this is fairly easy to explain: The post-increment operator is supposed to return the current value of the object and then increment it. Hence, we store the current value in a temporary variable, increment the members and then return the temporary variable (which holds the previous value of the object).
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.
Posts: 1,907
Time spent in forums: 1 Month 1 Week 1 Day 6 h 33 m 15 sec
Reputation Power: 583
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.
__________________
Joel B Fant
"An element of conflict in any discussion is a very good thing. Shows everybody's taking part, nobody left out. I like that."