By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

Taking IF sentences out of loops

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.