i < x or i != x?

This is a discussion on i < x or i != x? within the C Programming forums, part of the General Programming Boards category; Which would be the best/fastest way for evaluating the loop condition, Code: while (i &lt; x) or Code: while (i ...

  1. #1
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeå, Sweden
    Posts
    555

    i < x or i != x?

    Which would be the best/fastest way for evaluating the loop condition,
    Code:
    while (i < x)
    or
    Code:
    while (i != x)
    ? Assuming that the loop is incrementing i for each iteration, to avoid elitist remarks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,037
    Well assuming you know that i will at some point be exactly x, then there is no difference. But consider any possibility that might result in i never equaling x. Such as this:

    Code:
    float i = 0.0;
    while (i != 1.0) {
      printf("%.2f\n", i);
      i += .1;
    }
    Last edited by SlyMaelstrom; 03-04-2006 at 08:28 PM. Reason: Cause I had a for loop last time
    Sent from my iPad®

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeå, Sweden
    Posts
    555
    Yes, the value of i will be the same as x's at some point, they are both integers and i is incrementing by one at a time.

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by OnionKnight
    Which would be the best/fastest way for evaluating the loop condition,
    Code:
    while (i < x)
    or
    Code:
    while (i != x)
    ? Assuming that the loop is incrementing i for each iteration, to avoid elitist remarks
    I'm not an assembly expert, but I know on at least one architecture (ARM) those will both take exactly the same amount of time; cmp and then beq or bge (or bne vs blt); half a dozen in one hand and 6 in the other.

    But even if some architectures have one compare go faster than the other, don't give it any thought. Micro-optimization is bad. Forget "fastest" and just concentrate on "best" - whatever makes sense, is easy to understand and maintain. Always go good coding style.
    hello, internet!

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeå, Sweden
    Posts
    555
    Yes I usually leave stuff like that up to the compiler but I figured that != would be the same as ! and ==, two operations whereas < would be just one but it seems like they actually do the same amount of work.
    I've come to the conclusion that I'll use != in loops where the value will become the other at some point and < otherwise.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Posts
    5,439
    just for fun:

    Code:
    int
    main(void)
    {
    	unsigned i, start, stop, first, second, max = UINT_MAX;
    	i = 0;
    	start = clock();
    	while(i < max) i++;
    	stop = clock();
    	first = stop - start;
    	i = 0;
    	start = clock();
    	while(i != max) i++;
    	stop = clock();
    	second = stop - start;
    	cout << "(i < max): " << first << endl;
    	cout << "(i != max): " << second << endl;
    }
    the first one seems to execute *slightly* faster than the second on my system, but it's negligible. I'd say follow moi's advice.
    Code:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <( ' '<) Simple Programming Question?
    By strigen in forum C Programming
    Replies: 1
    Last Post: 03-05-2009, 02:17 PM
  2. Overloading == and < operator
    By cannsyl in forum C++ Programming
    Replies: 3
    Last Post: 11-23-2008, 09:21 PM
  3. x = x < y < 2; WHY IS THIS ALWAYS TRUE!?
    By sh4k3 in forum C Programming
    Replies: 5
    Last Post: 06-08-2007, 01:00 AM
  4. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 01:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21