Thread: How to optimize C++ code in Linux?

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    How to optimize C++ code in Linux?

    Hey All,

    I'm not sure if this is the right place (do I put this in Linux? Tech?) but I'm trying to profile my software to identify bottlenecks or where my code is the slowest.

    I just googled around and found out about gprof. I tried using it and the results don't seem that straight forward. Are there any other good tools or tips anyone may have about how to optimize code?

    Keep in mind, this is code that was first written to work so I wasn't expecting anything great but when I try to make it lean, it's not where I want it to be.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I would suggest you write a simple program and then try and gprof that as well.

    Eg.
    Code:
    void copy1 ( char *dest, const char *src ) {
      for ( i = 0 ; i <= strlen(src) ; i++ ) dest[i] = src[i];
    }
    void copy2 ( char *dest, const char *src ) {
      int len = strlen(src);
      for ( i = 0 ; i <= len ; i++ ) dest[i] = src[i];
    }
    void copy3 ( char *dest, const char *src ) {
      while ( (dest[i] = src[i]) != 0 ) i++;
    }
    gprof gives you a lot of info, so it takes time to learn how to use that data effectively.

    What you're basically looking for are counts of function invocations which are much larger than the size of the input problem, or times which seem much larger than the amount of work being done.

    Oh, and one more thing.
    You'll need an effective test suite before you start messing about with the code trying to make it quicker.
    After each "improvement", you need a way to be able to quickly tell whether it still WORKS, regardless of whether it is any quicker or not.
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    I'm not sure if this is the right place (do I put this in Linux? Tech?) but I'm trying to profile my software to identify bottlenecks or where my code is the slowest.
    This forum is usually for questions concerning the language itself, not tools, compilers, OS-specific API, etc.
    So to answer your question: the most appropriate place would be the Linux section.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Good tools? The best tool you have is your mind.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Hodor View Post
    Good tools? The best tool you have is your mind.
    True, but everyone uses that tool differently, so quality of results vary from the dreadful to the sublime.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Hodor View Post
    Good tools? The best tool you have is your mind.
    He who works without tools is a fool.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Different Logic to optimize this code further.
    By jagu in forum C Programming
    Replies: 2
    Last Post: 10-09-2011, 12:45 PM
  2. How can i optimize this code
    By ArunS in forum C Programming
    Replies: 15
    Last Post: 08-08-2011, 02:11 PM
  3. Please help me optimize my code
    By lazyme in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2010, 04:05 AM
  4. Replies: 6
    Last Post: 12-19-2007, 12:24 PM
  5. Wanna Optimize my code?
    By Echo in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2005, 01:24 AM