Taking IF sentences out of loops

L

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.
Big thanks,

LuTHieR
 
W

W Marsh

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.
 
A

Alf P. Steinbach

* 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>();
}
};
 
L

LuTHieR

Alf said:
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
 
J

Joe Gottman

LuTHieR said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top