fast code?

This is a discussion on fast code? within the C Programming forums, part of the General Programming Boards category; Where can I read about writing faster C code. For example, today I saw in a book something about the ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    46

    fast code?

    Where can I read about writing faster C code.

    For example, today I saw in a book something about the type double_t that is defined in c99. Apparently this type is optimized for floating point stuff.

    I know about inlining and the -O3 flag but there must be more info about this stuff. Like having the compiler unroll loops...

    (I compile with gcc on Mac OS X and I plan on always using the -std=c99 flag.)

    Thanks,
    Peter

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > Apparently this type is optimized for floating point stuff.
    But unless all your program does is floating point to excess, it's not likely to help you.

    > but there must be more info about this stuff. Like having the compiler unroll loops...
    For those not reading the manual, it's -funroll-loops
    gcc has a vast number of options, and many of them are a two-edged sword. Sometimes, they make things worse.

    > (I compile with gcc on Mac OS X and I plan on always using the -std=c99 flag.)
    Then you should also check out the -pg option and the gprof program

    When you have a completed program, and a representative set of input data, then you can start to tackle the optimisation problem. Trying to do anything before then is really a waste of time, because all you do is make guesses. For all you know, the program could be quick enough when you've finished anyway so there would be no real point.

    You make real measurements with a real program and real data, and analyse where the time is being spent (this is what gprof will tell you).

    You then look at the busy areas of the code and decide what (if anything) can be done about it. Then you can make a controlled change and rerun the tests, and compare the results to see if your change had any positive effect.

    http://win32.planet-d.net/opti/opti_c.htm
    If you manage to take care of the first two points, various gcc options will take care of the rest.
    Read the gotcha's at the end to determine how much of your time you should spend optimising.

    Here's another example, if your program is a big number cruncher which takes an hour to run, and you can save 5 minutes, its probably not worth optimising. Sure 5 mins sounds a lot, but users are just going to run it and goto lunch knowing that it will take about an hour.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    Thanks for the reply. Great info! I'll try to install gpof and try it out.

    Peter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  4. making programms faster
    By deian in forum C Programming
    Replies: 23
    Last Post: 10-09-2004, 12:19 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21