473,888 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1594
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 updateProgressM eter();
template<> void updateProgressM eter<true>() { out( '*' ); }
template<> void updateProgressM eter<false>() {}

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

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 updateProgressM eter();
template<> void updateProgressM eter<true>() { out( '*' ); }
template<> void updateProgressM eter<false>() {}

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

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.goo glegroups.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
4393
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 of the current sentence? Sound confusing? Well perhaps an example would help? Given... "Different countries have different ideas. Merry Christmas to all."
15
29827
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 you will see a sentence and then the tag.<span style='mso-spacerun: yes'> </span>Well, this next sentence will occur two spaces later because there are two spaces in the span. How to do this without using MS's style? Is there some way to...
2
1276
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
1593
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
2620
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 = null; for(int i=0;i<table_rowNums.Rows.Count; i++) { iRowNum = Convert.ToInt32(table_rowNums.Rows); //this table has all row numbers that will be modified DataRow row_1 = DT_tempTable.Select("col_row = " +
17
12186
by: Umesh | last post by:
Please try to do it while I try myself!
1
2070
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 have no idea since i have no one to ask. would someone give me some ideas and samples so that i can study it on my own. i need it badly. thank you very much for all the help. i would really appreciate it. thank you. example: user inputs a 1 or more...
1
1459
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 times the normal run times. However certain procedures in SQL 2005 run much quicker. On observation , it is found that the procedures having loops , cursors take more time in SQL 2005 . Is there some setting to be done to enable SP’s having loops...
3
5186
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 middle initial, but want to split after 'name') The company is is called H.R. Block. They are a good company. ( I don't want it to split after 'H', 'R') Here is the code i have, but it causes a split at every period. can someone help me out, im...
2
1407
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 them?plzz help me,its urgent.waiting for reply,bye
0
9961
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9800
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10886
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5819
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.