473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Loop Iterations Counter; Does It Exist?

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
Jun 27 '08 #1
6 1575
drmario wrote:
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
Jun 27 '08 #2
drmario wrote:
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 */
}
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


--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #3
drmario wrote:
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".
Jun 27 '08 #4
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:fv**********@aioe.org...
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.
>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".
Will do.
>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.
That sucks.

Thanks for the help.
Jun 27 '08 #5
"Juha Nieminen" <no****@thanks.invalidwrote in message
news:bo************@read4.inet.fi...
drmario wrote:
>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".
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?
Jun 27 '08 #6
drmario wrote:
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:fv**********@aioe.org...
>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.
[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
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Edwards | last post by:
Hello, I have sort of a newbie question. I'm trying to optimize a loop by breaking it into two passes. Example: for(i = 0; i < max; i++) {
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
7
by: Daniel Vallstrom | last post by:
I am having trouble with floating point addition because of the limited accuracy of floating types. Consider e.g. x0 += f(n) where x0 and f are of some floating type. Sometimes x0 is much larger...
18
by: Derek Basch | last post by:
What is the best way to count nested loop iterations? I can only figure to use an index but that seems kludgy. index = 0 for animal in zoo: for color in animal: index += 1 Thanks, Derek...
12
by: poornimamprabhu | last post by:
Hi there, suppose i have piece of code like main() { int i,j; for(i=0;i<10;i++){ int k = i; printf("%d %p = *%d\n",i,&k,k); }
18
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.