Re: Wasted CPU Cycles

Discussion in 'C++' started by tni, Mar 3, 2011.

  1. tni

    tni Guest

    On 2011-03-03 16:17, Joe Hesse wrote:
    > I occasionally find myself writing C/C++ functions where something is
    > only done once but the test occurs multiple times. Below are two
    > examples. Does anyone have suggestions to make the code better?


    What you shouldn't worry about are wasted CPU cycles. That kind of
    branch is predictable and correctly predicted branches are free (take 0
    time) or close to free on most current CPUs.

    Modern CPUs generally have pretty sophisticated branch prediction, so if
    you had a low iteration count in your second case, the branch prediction
    will likely predict all branches correctly when your function is called
    after the first time.

    A workaround that results in larger code is likely going to be a larger
    waste of CPU cycles (larger code = more cache misses which are expensive).
    tni, Mar 3, 2011
    #1
    1. Advertising

  2. tni

    Puppet_Sock Guest

    On Mar 3, 11:35 am, tni <> wrote:
    > On 2011-03-03 16:17, Joe Hesse wrote:
    >
    > > I occasionally find myself writing C/C++ functions where something is
    > > only done once but the test occurs multiple times.  Below are two
    > > examples.  Does anyone have suggestions to make the code better?

    >
    > What you shouldn't worry about are wasted CPU cycles. That kind of
    > branch is predictable and correctly predicted branches are free (take 0
    > time) or close to free on most current CPUs.

    [snip]

    Or rather, if you must worry about it, you should
    be doing it with accurate information. For example,
    get the original code and a stop watch. Then get
    some candidate alternatives. Run each possible
    version and measure how long it takes. Make sure
    you use sensible candiate data as inputs, not just
    the shortest possible run.

    Better yet, get some good profiling tools and see
    how long it really spends doing each version.

    If different versions take the same time, don't
    worry about it. Do what makes the code more
    clear, easier to document, easier to understand,
    easier to test, and such like "motherhood" things.

    Then back up a step or two and see if you have a
    specification on how fast it must run. If there
    is no spec on execution time, or if there is and
    you are easily meeting it, then don't sweat a few
    extra cycles here and there even if they are being
    wasted. Get the code right first. Keep all those
    motherhood things in mind while doing it. And
    only after all of that think about optimizing.

    If you optimize first you will often get bad code.

    Clear, easy to understand, easy to test, easy to
    document code is a value. It's also easier to try
    out optimizing alternatives on such code.

    Similar comments apply to things like trying to
    make the code smaller, use less memory, less lines
    of code, etc. If *every* other measure of code
    quality were identical then a smaller code is
    likely better. But shorter isn't the only concern.
    And in many cases it isn't the first concern. Or
    even in the top few.
    Socks
    Puppet_Sock, Mar 3, 2011
    #2
    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. -
    Replies:
    7
    Views:
    321
    Kevin McMurtrie
    Mar 22, 2005
  2. Russ
    Replies:
    4
    Views:
    401
    Piotr Gaertig /Gertas/
    May 3, 2005
  3. Kevin Klein
    Replies:
    4
    Views:
    626
    Dan Pop
    Apr 2, 2004
  4. Kevin Klein
    Replies:
    2
    Views:
    884
    those who know me have no need of my name
    Apr 2, 2004
  5. Kevin Klein

    How many CPU cycles does an instruction take ?

    Kevin Klein, Apr 1, 2004, in forum: C Programming
    Replies:
    4
    Views:
    1,189
    Dan Pop
    Apr 2, 2004
Loading...

Share This Page