Loop optimization

H

haroon

Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?

1- for (i = 0; str[i++] != '\0';) ;

2- for (i = 0; str[] != '\0'; ++i) ;
 
H

haroon

the second loop statement should be read as:

for (i = 0; str != '\0'; ++i) ;

sorry for inconvenience.

Haroon
 
A

Alex Fraser

haroon said:
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?

1- for (i = 0; str[i++] != '\0';) ;
2- for (i = 0; str[] != '\0'; ++i) ;

It depends on the compiler, amongst other things. It would not surprise me
if exactly the same code was generated for both (ie both were equally
efficient).

Alex
 
D

David Resnick

haroon said:
the second loop statement should be read as:

for (i = 0; str != '\0'; ++i) ;

sorry for inconvenience.

Haroon


C language standard says nothing about efficiency, so
not a lot to say here. You can test them on your machine and
see which is faster. The two loops end up with i at different
values, so perhaps "which does what you want" would be
a better place to start. That said, the standard strlen
MIGHT be faster, as it may use platform specific tricks
(fancy assembly instructions for determining string length?)
not available in standard C.

-David
 
P

pete

haroon said:
the second loop statement should be read as:

for (i = 0; str != '\0'; ++i) ;


In that case, I believe that Alex Fraser
has said all that there is to say on this matter.
 
E

Eric Sosman

haroon said:
Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?

1- for (i = 0; str[i++] != '\0';) ;

2- for (i = 0; str[] != '\0'; ++i) ;

Version 2 is more efficient, because it won't compile
and will therefore consume no time at all in execution.

More seriously, your question is ill-posed. If we
correct the second version by changing `str[]' to `str'
(I'm just guessing, but it's your error that forces me to
make the guess), observe that the two loops do not perform
the same task. Since they perform different tasks, it is
senseless to ask about their relative efficiency. Which
is more efficient: A bicycle or a banana?

Personally, I don't think I'd write either loop. On
the supposition that `str' is a `char' array containing a
string (or is a pointer to the start of a string), I'd more
likely write

i = strlen(str) + 1; /* version 1 */
i = strlen(str); /* version 2, corrected */

.... depending on the desired outcome. I'd do so even though
there's no guarantee that strlen() is more efficient than
your loops, because it is surpassingly unlikely that the
calculation of a string length is the bottleneck in the
program. If it does turn out to be the bottleneck, you'll
discover that only by measurement -- and the measurement (and
whatever improvements might be possible) will be specific to
the particular C implementation you're using at the moment.
 
J

Jordan Abel

Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?

1- for (i = 0; str[i++] != '\0';) ;

2- for (i = 0; str[] != '\0'; ++i) ;

There's no particular reason to think either would be more or less
efficient.

Now, one which might more plausibly be more efficient would be

char *s = str; while(*s++) ... ;

but that depends a lot on what you're doing.
 
C

carl

Sorry: for( ; str != '\0'; ++i)
If this is what you intend.
I believe this would be ok:

unsigned int i = 0;
for( ; str; ++i)

Depends on CPU, what instructions are built-in and whether your
compiler has unrolled the loop.

Have you declared 'i' as unsigned?
http://www.abarnett.demon.co.uk/tutorial.html#INTEGERS

Maybe use prefix, even though this means to references to i:
http://www.tantalon.com/pete/cppopt/asyougo.htm#UsePrefixOperators

the second loop statement should be read as:

for (i = 0; str != '\0'; ++i) ;

sorry for inconvenience.

Haroon
 
C

Chris Dollin

it is more efficient with pointer

What is?
haroon said:
Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?

1- for (i = 0; str[i++] != '\0';) ;

2- for (i = 0; str[] != '\0'; ++i) ;

Oh, /that/. How do you know it's more efficient with pointers? You
certainly can't tell from the C standard.
 
R

Randy Howard

haroon wrote
(in article
Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?

1- for (i = 0; str[i++] != '\0';) ;

2- for (i = 0; str[] != '\0'; ++i) ;

It depends. It always depends. On the compiler usually,
sometimes upon other factors beyond your control. The correct
answer is usually to allow the compiler to do the optimization
for you (by setting the appropriate flags for your target
platform) and choose the one that is most readable or easiest to
maintain.

In this particular case, I would personally opt for neither of
the above.
 
F

Flash Gordon

it is more efficient with pointer

1) You response belongs *under* the text you are replying to, not above.
2) Have you proved his by measuring the performance of the code
generated by *all* compilers?
3) It is easy for a compiler to translate from the array version to the
pointer version and so the pointer version is unlikely to produce a
faster executable.
4) Many compilers will produce the same code whether you use pointer
arithmetic or arrays.
haroon said:
Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?

1- for (i = 0; str[i++] != '\0';) ;

2- for (i = 0; str[] != '\0'; ++i) ;
 
F

Flash Gordon

carl said:
I believe this would be ok:

You reply belongs under the text you are replying to, not above.
unsigned int i = 0;
for( ; str; ++i)


You can't put that in the middle of a code block.
Depends on CPU, what instructions are built-in and whether your
compiler has unrolled the loop.

True, what is most efficient depends on you compiler and *changes* from
one version to the next as they change the optimiser.

I've yet to see a system where using unsigned integers was guaranteed to
be faster. Generally on 2s complement systems (i.e. almost all in
current production) the processor will do exactly the same in the same
amount of time whether i is signed or unsigned.
Maybe use prefix, even though this means to references to i:
http://www.tantalon.com/pete/cppopt/asyougo.htm#UsePrefixOperators

That applies to objects in C++, not to simple integer variables in C (or
probably C++). Also, I would be surprised to see any vaguely modern
compiler (a version released within the last 10 years) that produced
different code for i++ and ++i when used on an integer in a void context
(i.e. the value of the expression is not used).
haroon said:
the second loop statement should be read as:

for (i = 0; str != '\0'; ++i) ;

sorry for inconvenience.

Haroon

 
I

Ian Malone

Flash said:
(e-mail address removed) wrote:

>> haroon schrieb:
>>
>>> Hi,
>>> I have two versions of a loop. I want to know which one is more
>>> efficient. and is there any way to make it even more efficient?
>>>
>>> 1- for (i = 0; str[i++] != '\0';) ;
>>>
>>> 2- for (i = 0; str[] != '\0'; ++i) ;
>>
>>
>
>
it is more efficient with pointer


1) You response belongs *under* the text you are replying to, not above.
2) Have you proved his by measuring the performance of the code
generated by *all* compilers?
3) It is easy for a compiler to translate from the array version to the
pointer version and so the pointer version is unlikely to produce a
faster executable.
4) Many compilers will produce the same code whether you use pointer
arithmetic or arrays.

I /think/ he may be referring to the fact that an int is being
incremented, so you are doing increment then pointer arithmetic,
rather than incrementing a pointer. Of course this is a common
idiom that may well get optimised out anyway.
 
I

Ian Malone

Ian said:
Flash said:
(e-mail address removed) wrote:

haroon schrieb:

Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?

1- for (i = 0; str[i++] != '\0';) ;

2- for (i = 0; str[] != '\0'; ++i) ;
it is more efficient with pointer



1) You response belongs *under* the text you are replying to, not above.
2) Have you proved his by measuring the performance of the code
generated by *all* compilers?
3) It is easy for a compiler to translate from the array version to the
pointer version and so the pointer version is unlikely to produce a
faster executable.
4) Many compilers will produce the same code whether you use pointer
arithmetic or arrays.

I /think/ he may be referring to the fact that an int is being
incremented, so you are doing increment then pointer arithmetic,
rather than incrementing a pointer. Of course this is a common
idiom that may well get optimised out anyway.

Addendum: there's no way to tell from the code given whether str is
declared as a pointer or an array anyway.
 
F

Flash Gordon

Ian said:
Flash said:
(e-mail address removed) wrote:
haroon schrieb:

Hi,
I have two versions of a loop. I want to know which one is more
efficient. and is there any way to make it even more efficient?

1- for (i = 0; str[i++] != '\0';) ;

2- for (i = 0; str[] != '\0'; ++i) ;
it is more efficient with pointer
2) Have you proved his by measuring the performance of the code
generated by *all* compilers?
3) It is easy for a compiler to translate from the array version to the
pointer version and so the pointer version is unlikely to produce a
faster executable.
4) Many compilers will produce the same code whether you use pointer
arithmetic or arrays.

I /think/ he may be referring to the fact that an int is being
incremented, so you are doing increment then pointer arithmetic,
rather than incrementing a pointer.

I'm sure that was what he meant.
> Of course this is a common
idiom that may well get optimised out anyway.

That was part of what I meant in points 3 and 4.
 
C

carl

Flash said:
That applies to objects in C++, not to simple integer variables in C (or
probably C++). Also, I would be surprised to see any vaguely modern
compiler (a version released within the last 10 years) that produced
different code for i++ and ++i when used on an integer in a void context
(i.e. the value of the expression is not used).

Interesting, I thought the two did produce different code, isn't this
true:

prefix op {
x = x + 1;
return x;
}

postfix op {
int tmp = x;
x = x + 1;
return tmp;
}

What does 'void context' mean and does this change the above code?

I'm new to this and would like to know whether I just wasted loads of
time changing my postfix to prefix in a futile attempt at optimisation!


TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,475
Latest member
NovellaSce

Latest Threads

Top