473,396 Members | 1,724 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,396 software developers and data experts.

Taking IF sentences out of loops

Hi,
I would like your help about this: suppose you have a method in a class
which basically consists of a big for loop which has to do a lot of
iterations. This method accepts a boolean parameter which is true if
you have to update a progress bar on each iteration.
Is there any way of doing it which not involves doing an if comparison
on each iteration of the loop? Because doing it like that implies a LOT
of unnecessary comparisons.
Big thanks,

LuTHieR

Jun 11 '06 #1
4 1578
On 11 Jun 2006 03:30:00 -0700, "LuTHieR" <gr*************@gmail.com>
wrote:
Hi,
I would like your help about this: suppose you have a method in a class
which basically consists of a big for loop which has to do a lot of
iterations. This method accepts a boolean parameter which is true if
you have to update a progress bar on each iteration.
Is there any way of doing it which not involves doing an if comparison
on each iteration of the loop? Because doing it like that implies a LOT
of unnecessary comparisons.
Big thanks,

LuTHieR


We don't know.

Is there?

Show some complete and compilable code, please.
Jun 11 '06 #2
* LuTHieR:
Hi,
I would like your help about this: suppose you have a method in a class
which basically consists of a big for loop which has to do a lot of
iterations. This method accepts a boolean parameter which is true if
you have to update a progress bar on each iteration.
Is there any way of doing it which not involves doing an if comparison
on each iteration of the loop? Because doing it like that implies a LOT
of unnecessary comparisons.


First, /measure/ whether it actually affects performance in any way that
matters.

If it does, try your compiler's optimization switches, and /measure/ again.

If you still have an actual performance problem, try something like

void out( char c ) { std::cout << c << std::flush; }

class Foo
{
private:
template< bool feedback > void updateProgressMeter();
template<> void updateProgressMeter<true>() { out( '*' ); }
template<> void updateProgressMeter<false>() {}

public:
template< bool showProgress >
void bar()
{
for( int i = 1; i <= 5; ++i )
{
updateProgressMeter<showProgress>();
}
}

void bar( bool showProgress )
{
showProgress? bar<true>() : bar<false>();
}
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 11 '06 #3
Alf P. Steinbach wrote:

First, /measure/ whether it actually affects performance in any way that
matters.

If it does, try your compiler's optimization switches, and /measure/ again.

If you still have an actual performance problem, try something like

void out( char c ) { std::cout << c << std::flush; }

class Foo
{
private:
template< bool feedback > void updateProgressMeter();
template<> void updateProgressMeter<true>() { out( '*' ); }
template<> void updateProgressMeter<false>() {}

public:
template< bool showProgress >
void bar()
{
for( int i = 1; i <= 5; ++i )
{
updateProgressMeter<showProgress>();
}
}

void bar( bool showProgress )
{
showProgress? bar<true>() : bar<false>();
}
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Wow, thanks :)
And sorry for not having posted some compilable code, but it was a very
big code and I was looking for a general solution like the one Alf
provided. Anyway, thanks to you both.

LuTHieR

Jun 11 '06 #4

"LuTHieR" <gr*************@gmail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Hi,
I would like your help about this: suppose you have a method in a class
which basically consists of a big for loop which has to do a lot of
iterations. This method accepts a boolean parameter which is true if
you have to update a progress bar on each iteration.
Is there any way of doing it which not involves doing an if comparison
on each iteration of the loop? Because doing it like that implies a LOT
of unnecessary comparisons.


That depends on whether the loop can change the result of the IF
statement. If it can then you're out of luck and have to keep the IF inside
the loop. If it can't, then define a boolean variable outside the loop and
set it equal to the result of the IF.
bool foo = // whatever's inside your IF statement

Then, inside your query you can have

if (foo) // body of the if statement

Joe Gottman
Jun 11 '06 #5

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

Similar topics

4
by: Tony | last post by:
Hello, Can someone please point me toward a regular expression that goes through a string and contructs a list of sentences and part sentences, where words are gradually dropped from the front...
15
by: Randall Parker | last post by:
I've noticed when exporting from Microsoft Word XP into an HTML file that Word uses a span style of mso-spacerun: yes. This has the effect of making there be about 2 spaces between sentences. So...
2
by: devil_online | last post by:
Hi, I want to put sentences in a page...like start at 9pm and ads more sentences like 5 in 5minutes, not erasing the others. how can I do it in javascript? thanks
12
by: effendi | last post by:
Hi can anyone tell me what is the best way to determine the number of sentences that someone enter into a text area? Thanks in advanced.
3
by: VM | last post by:
Why is the third line of code in the loop take so long? W/o that line, the execution (35,000 loops) runs in 3 secs. With the line, it goes up to over 5 mins. dataGrid_auditAddress.DataSource =...
17
by: Umesh | last post by:
Please try to do it while I try myself!
1
by: icesha | last post by:
hello. good day. i am currently studying php on my own.and im just new to it. i have a problem on how to parse sentences inputted by the user into some sentence patter like subject - predicate. i...
1
by: anandanies | last post by:
I am having a new server which has SQL 2005 standard edition SP2 installed on it (OS windows 2003 server) On migrating from SQL 2000 to SQL 2005 , I observe certain procedures taking almost 10...
3
by: mark1491 | last post by:
I am trying to split a string into sentences with preg_split, but I would like it to not split initials. Examples: Mark H. Doolittle is my name. What is yours. ( I don't want it to split the...
2
by: litun | last post by:
hi i want to arrange the sentences as per their line numbers,i.e,in ascending order.i have the line numbers stored in an array list and the sentences in another array list.how can i arrange...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.