#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    21
    Rep Power
    0

    Speeding your code


    well this is more of a general type of question and discussion but I put it here since I am using C.

    How and which strategies do you guys use if you want to speed up the execution of a piece of code?

    Now I have some code here (and yes it is a mess, the guy who programmed it had no idea of what modularity, encapsulation or such things were) and I ve been asked to speed it up, say 3 or 4 miliseconds at least.

    First I have heard but have no idea about tools to measure performance. so I just use the old "log" resource to measure the times of execution

    then I have a lot of "ifs " and "else ifs" and I ve told not to modify the order of condition testing.

    I am lost really. So far I have been thinking of using pointer shifting to replace copying the contents of a huge array that I suppose it takes time. I figure that if instead of copying the whole array I just shift a pointer from here to there, it would increase speed.

    So anyway, in your experience , what things help for this or is there any resource in which I can read about this?

    I am working in C with a SuperH processor btw

    Kansai
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Code profiling shows where to devote your resources.

    Once you've found which parts of code you should focus on, the next question might be "what can I do about it?"

    I don't know before seeing the code. I'll offer two real world stories:

    I once wrote a program that ran in about an hour. My manager said, "no no, that's much to slow". Profiling showed my code was recomputing coefficients in an interpolation routine that I had thought was "pretty fast". Caching the coefficients reduced the run time to a few seconds, and I had resolved the issue within the same day.

    We had another code that was a direct translation from matlab to FORTRAN. I reduced the run time by rewriting the complex arithmetic using algebraic identities, all the while using tests to make sure I hadn't {expletive deleted} up. Profiling identified the hot spot.

    Comments on this post

    • salem agrees : Nicely put
    • Lux Perpetua agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    My sig contains a writeup on performance programming, it might be worth your while. As b49P23TIvg said there is no point in performance optimization unless you actually have a performance problem, so profiling is critical (unless you have the experience to recognize bottlenecks just by reading the code, in which case I doubt you would be seeking help here). If you are going to refactor the code anyway, leave off any optimizations until the code has been refactored and thoroughly tested (you _must_ produce a comprehensive regression test suite before you start refactoring OR optimization!). If you aren't going to refactor (probably a bad idea based on what you have said), do profiling first to identify where (if there are any) bottlenecks and focus your time there.

    Comments on this post

    • Lux Perpetua agrees

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Finishing the story, it was about a decade later that I discovered what actually made my program fast. My solver never had convergence troubles, which unknown to me plagued other codes. One of the dependent variables was a population fraction (of atoms in an excited state, not zebras on the plain) with physical limits from 0 to 1. When writing the code, without much deliberation, I asked "What if I get impossible results here?". To which I answered, "I'll just clip the solution between 0 and 1." I did, and didn't think about it again for 10 years.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo