#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Location
    Noida/India
    Posts
    127
    Rep Power
    10

    Question which of these for loop styles is more efficient ( was "Plz Help...." )


    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. #2
  3. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep 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.

    Fourth - have you tried benchmarking it yourself?

    --Simon
  4. #3
  5. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1051
    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.

    Comments on this post

    • Yawmark agrees
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    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
    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.

    Comments on this post

    • Axweildr agrees : patience is a virtue, as you grow older, you will find memory moreso ...
    • netytan agrees : The ever brilliant Scorpi everyone :)
    • medialint agrees
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    Note that in Java (1.5), there is no difference in the bytecode:

    Java Code:
    public void pre() {
        for(int i = 0; i < 10; ++i);
    }
     
    // public void pre();
    //   Code:
    //    0:	iconst_0
    //    1:	istore_1
    //    2:	iload_1
    //    3:	bipush	10
    //    5:	if_icmpge	14
    //    8:	iinc	1, 1
    //    11:	goto	2
    //    14:	return
     
    public void post() {
        for(int i = 0; i < 10; i++);
    }
     
    // public void post();
    //   Code:
    //    0:	iconst_0
    //    1:	istore_1
    //    2:	iload_1
    //    3:	bipush	10
    //    5:	if_icmpge	14
    //    8:	iinc	1, 1
    //    11:	goto	2
    //    14:	return
    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)));}}
  12. #7
  13. Arcane Scribbler
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2005
    Location
    Indianapolis, IN
    Posts
    1,907
    Rep Power
    585
    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]

    [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]

    [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[/code]

    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."

    .NET Must-Haves
    Tools: Reflector
    References: Threading in .NET

IMN logo majestic logo threadwatch logo seochat tools logo