Like Tree5Likes
  • 1 Post By Salem
  • 1 Post By Hodor
  • 1 Post By grumpy
  • 2 Post By brewbuck

How to optimize C++ code in Linux?

This is a discussion on How to optimize C++ code in Linux? within the Linux Programming forums, part of the Platform Specific Boards category; Hey All, I'm not sure if this is the right place (do I put this in Linux? Tech?) but I'm ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    stahta01 likes this.
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    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
    Registered User Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    650
    Good tools? The best tool you have is your mind.
    MutantJohn likes this.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,306
    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.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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.
    Elysia and Hodor like this.
    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, 03:05 AM
  4. Replies: 6
    Last Post: 12-19-2007, 11:24 AM
  5. Wanna Optimize my code?
    By Echo in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2005, 01:24 AM

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