Loop Iterations Counter; Does It Exist?

D

drmario

Here's what I got:

ifstream File; // already initialized and associated with a file
input stream
unsigned short r = 1; // a counter to tell me if this is the loop's first
iteration

do {
if( (File.peek() == 32) && r)
return 0;
else if ... snip
else ... snip
r=0;
} while(some condition)

I've done this because I want to know if my function encounters the newline
character before everything else. If it does, I need it to return false.
If it doesn't, I need it to make r=0, so that on the next iteration if it
happens to find a newline char, but it's not the first iteration, it won't
return false.

What I don't like is that let's say 100 iterations of this loop happens
before it returns anything. I'm wasting processor time by assigning 0 to r
every single iteration.

Therefore my question is: is there not a way for me to change that if ( )
statement up there so that I can somehow see if this is the first iteration
of the do...while() loop, WITHOUT having to use a variable and waste
processor time assigning it all the time?

thanks
Mario
 
K

Kai-Uwe Bux

drmario said:
Here's what I got:

ifstream File; // already initialized and associated with a file
input stream
unsigned short r = 1; // a counter to tell me if this is the loop's
first iteration

do {
if( (File.peek() == 32) && r)
return 0;
else if ... snip
else ... snip
r=0;
} while(some condition)

I've done this because I want to know if my function encounters the
newline
character before everything else. If it does, I need it to return false.
If it doesn't, I need it to make r=0, so that on the next iteration if it
happens to find a newline char, but it's not the first iteration, it won't
return false.

What about:

if ( File.peek() == 32 ) {
return ( 0 );
}
do {
if ... snip
else ... snip
} while ( some condition );

What I don't like is that let's say 100 iterations of this loop happens
before it returns anything. I'm wasting processor time by assigning 0 to
r every single iteration.

Google "premature optimization".

Therefore my question is: is there not a way for me to change that if ( )
statement up there so that I can somehow see if this is the first
iteration of the do...while() loop, WITHOUT having to use a variable and
waste processor time assigning it all the time?

No. There is no built-in way to check whether you are in the first iteration
of a loop.


Best

Kai-Uwe Bux
 
J

Jim Langston

drmario said:
Here's what I got:

ifstream File; // already initialized and associated with a
file input stream
unsigned short r = 1; // a counter to tell me if this is the
loop's first iteration

do {
if( (File.peek() == 32) && r)
return 0;
else if ... snip
else ... snip
r=0;
} while(some condition)

if ( File.peek() == 32 )
return 0;

do {
/* your loop here */
}
 
J

Juha Nieminen

drmario said:
I'm wasting processor time by assigning 0 to r
every single iteration.

Your File.peek() probably takes hundreds of clock cycles. That
assignment doesn't have any significant slowing effect compared. Trying
to remove it would be completely useless "optimization".
 
D

drmario

Kai-Uwe Bux said:
What about:

if ( File.peek() == 32 ) {
return ( 0 );
}
do {
if ... snip
else ... snip
} while ( some condition );

It would seem that all you've done there is remove the "&& r" part of the if
condition. That will not work for me because checking whether or not r is 1
(I think I forgot to mention, I initialize r to 1 when its declared) is part
of the decision whether or not to return 0.
Google "premature optimization".

Will do.
No. There is no built-in way to check whether you are in the first
iteration
of a loop.

That sucks.

Thanks for the help.
 
D

drmario

Juha Nieminen said:
Your File.peek() probably takes hundreds of clock cycles. That
assignment doesn't have any significant slowing effect compared. Trying
to remove it would be completely useless "optimization".

So in other words, *in this particular case* don't worry about it since
removing 100 repetitions of r=0 would make only an infintesmal difference?
 
K

Kai-Uwe Bux

drmario said:
It would seem that all you've done there is remove the "&& r" part of the
if condition.
[snip]

Have a closer look: I also moved the check in front of the loop. Therefore,
the return(0) clause will only hit in what was the 1st iteration of your
code. In fact, the version I gave is completely equivalent to yours (and a
good optimizing compiler might even create identical object code from it).

For context and comparison, here is your code:
unsigned short r = 1;
do {
if( (File.peek() == 32) && r)
return 0;
else if ... snip
else ... snip
r=0;
} while(some condition)


Best

Kai-Uwe Bux
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top