Taking IF sentences out of loops

Discussion in 'C++' started by LuTHieR, Jun 11, 2006.

  1. LuTHieR

    LuTHieR Guest

    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
     
    LuTHieR, Jun 11, 2006
    #1
    1. Advertising

  2. LuTHieR

    W Marsh Guest

    On 11 Jun 2006 03:30:00 -0700, "LuTHieR" <>
    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.
     
    W Marsh, Jun 11, 2006
    #2
    1. Advertising

  3. * 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?
     
    Alf P. Steinbach, Jun 11, 2006
    #3
  4. LuTHieR

    LuTHieR Guest

    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
     
    LuTHieR, Jun 11, 2006
    #4
  5. LuTHieR

    Joe Gottman Guest

    "LuTHieR" <> wrote in message
    news:...
    > 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
     
    Joe Gottman, Jun 11, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony
    Replies:
    4
    Views:
    2,185
    Andy De Petter
    Nov 27, 2003
  2. Guru Nathan via JavaKB.com

    Counting no.of sentences

    Guru Nathan via JavaKB.com, Feb 28, 2005, in forum: Java
    Replies:
    13
    Views:
    818
  3. Chris Beall

    Double space between sentences?

    Chris Beall, Jul 21, 2003, in forum: HTML
    Replies:
    15
    Views:
    38,932
    Jukka K. Korpela
    Jul 25, 2003
  4. Jim Cain
    Replies:
    1
    Views:
    229
    Yukihiro Matsumoto
    Jul 18, 2003
  5. Me
    Replies:
    2
    Views:
    263
Loading...

Share This Page