Thread: Optimization

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    Optimization

    It seems to me that if I were due to release some software, I would hand-optimize the code before running it through the compiler, where those optimizations would be purely for that release and not part of the main code repository. Once I had compiled and released the code, I would then continue to develop the main repository and only introduce the optimizations when compiling for release.

    Could a compiler have some kind of mega-optimization option, or could there be a separate optimization program, that could work for minutes or hours, and substantially reorganize the code, before giving it to a compiler? Or is it always better to optimize while compiling?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    No, that would be an utter waste of time.

    You should not be thinking optimization (except at a high level, like algorithm selection) when writing code. That is, it's much more important to select the proper data structure for a particular operation than it is to choose what order to write a few statements in.

    Modern compilers can do what is called link-time optimization, which optimizes the entire program, regardless of how many translation units it spans across. At least gcc (via -flto), Intel's compiler (-ipo), Solaris Studio (-xipo), Open64 (-ipa), and clang (-O4 with a suitable linker) support it. You might consider this to be a mega-optimization. LTO can have a great effect on the speed of your code, depending on how it's written. On a project of mine, I saw gains around 5% to 30% with gcc's LTO, depending on what operations were being done, mainly because the compiler was able to inline a lot of small functions across translation units.

    If you do want to optimize your code by hand (which is sometimes a useful endeavor), you should profile it first, and find where it's actually slow. Valgrind includes great profiling tools called called cachegrind and callgrind; you can view the latter's reports with kcachegrind, which is an amazingly useful visualization tool.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Richardcavell View Post
    It seems to me that if I were due to release some software, I would hand-optimize the code before running it through the compiler, where those optimizations would be purely for that release and not part of the main code repository.
    Did you know this idea has a name?

    It's called .... "Introducing untraceable errors".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. optimization and debugging options in Makefile
    By lehe in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2009, 01:34 PM
  2. Turn Off Optimization?
    By danlee58 in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 03:52 AM
  3. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM
  4. optimization flags
    By markucd in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2006, 09:08 AM
  5. Optimization settings
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-23-2005, 02:53 AM

Tags for this Thread