Thread: Program running slower after optimization?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    32

    Program running slower after optimization?

    Hello,

    I programmed a set of functions for bit output. Trying to make my code run even faster, I came across two lines of code that looked like this:

    Code:
    *put_buffer |= bits >> (count - current_bit);
     count -= current_bit;
    I replaced it with:

    Code:
     count -= current_bit;
    *put_buffer |= bits >> count;
    And did the same for two similar pieces of code.

    Than I ran my program in Time profiler (I'm coding on a Mac and using Xcode) only to find out that - on average - my program spent about 30% of time more in the function that contained the above changes.

    Any ideas why that might be happening? With that three changes, I did remove three subtractions for each function call and the function was called 10,000,000 times in each of my testing runs.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, was the revised program run as a release, or did you accidentally run it as a debug version?

    Would there be cache miss on the second example, for some reason?

    What differences does the generated assembly level code show? I'm not able to help much with assembly (to put it mildly), but when you get down to this level of optimization, I believe it becomes essential to consult it.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I think you would need to post the whole function in question (both versions).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the more new and delete operation, the slower they gets?
    By idleman in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2009, 11:16 AM
  2. Program optimization
    By a.mlw.walker in forum C Programming
    Replies: 1
    Last Post: 03-27-2009, 06:12 AM
  3. Gmail slower?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-27-2005, 11:26 PM
  4. slower loop 2nd time
    By bballzone in forum C++ Programming
    Replies: 9
    Last Post: 07-28-2004, 02:38 PM
  5. Linux's GUI slower than windows'....
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 02-16-2002, 08:47 PM