Re: Taking IF sentences out of loops
* 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?