Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Taking IF sentences out of loops

Reply
Thread Tools

Taking IF sentences out of loops

 
 
LuTHieR
Guest
Posts: n/a
 
      06-11-2006
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

 
Reply With Quote
 
 
 
 
W Marsh
Guest
Posts: n/a
 
      06-11-2006
On 11 Jun 2006 03:30:00 -0700, "LuTHieR" <(E-Mail Removed)>
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-11-2006
* 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?
 
Reply With Quote
 
LuTHieR
Guest
Posts: n/a
 
      06-11-2006
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

 
Reply With Quote
 
Joe Gottman
Guest
Posts: n/a
 
      06-11-2006

"LuTHieR" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
Counting no.of sentences Guru Nathan via JavaKB.com Java 13 03-03-2005 01:10 PM
regexp to list all sentences and sub sentences, with overlapping? Tony Perl 4 11-27-2003 01:38 PM
Word grammar checker doesn't catch sentences that do not end in a period Ima Goodlay Computer Support 3 10-08-2003 11:25 AM
Double space between sentences? Chris Beall HTML 15 07-25-2003 10:08 PM



Advertisments