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

    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    How to compare code for efficiency


    Hai,

    I have written two function to find the position of a substring.
    though the both functions are giving the same out put i would like to find out which one is efficient in terms of memory and execution speed is concerned.

    Please point me out in case of any logical errors in the above program and also let me know to find out the efficiency of a program

    By efficiency i mean
    The no of Assembly instructions the code generates
    The Memory utilization
    Speed of execution
    Unnecesary operations (for Optimization)


    Code:
     
    #define CHECK_POS_OF_SUB_STR(STR,SUBSTR)   if((p=pos(STR,SUBSTR)) >= 0 ){ \
             printf("pos of (%s) in (%s) is %d\n",SUBSTR,STR,p);\
        }\
        else if(p==-2) {\
            printf("(%s) is not a substr of  %s\n",SUBSTR,STR);\
        }\
        else {\
            printf("One of the strings is empty\n");\
        }
     char string[] = "aaabbbccdefgghijkklmnnopqrsstuvwxyyyzz";
     char substr1[]="ggghij";
     char substr2[]="ggghij";
     char substr3[]="ggghijk";
     char substr4[]="ggghijj"; 
     char substr5[]="xyyyyzzzz"
     char substr6[]="ccdex"
     char substr7[]="aaabbccdefggghijkklmnnopqrsstuvwxyyyzz";
    
     CHECK_POS_OF_SUB_STR(string,substr1);
     CHECK_POS_OF_SUB_STR(string,substr2);
     CHECK_POS_OF_SUB_STR(string,substr3);
     CHECK_POS_OF_SUB_STR(string,substr4);
     CHECK_POS_OF_SUB_STR(string,substr5);
     CHECK_POS_OF_SUB_STR(string,substr6);
     CHECK_POS_OF_SUB_STR(string,substr7);
    code 1:
    Code:
    int pos( char *string , char *substring)
     {
         char *sub = substring;
         char *str = string;
     
         if(!string || !substring){
             return -1;
         }
         while( *str )
         { 
             if(*str == *sub)
             {
                 char *to = str;
                 while(*sub && (*to == *sub))
                     to++, sub++;
                 if(!*sub)
                     return str - string;//start pos of substr
             }
             str++;
             sub=substring;
         }
         return -2;// substr is not there
     }
    code 2:
    Code:
    int pos( char *str , char *sub)
    {
        char *m = str;;
        char *sstr = sub;
        int cnt = 0;
    
        if (!str || !sub)
        {
            return -1;
        }
    
        while( *str )
        {
            if(*sstr && (*str != *sstr))
            {
                str++;
                if(cnt){
                    sstr = sub;
                    cnt = 0;
                }
            }else if(*sstr){
                str++,sstr++;
                cnt++;
    
            }else{
                return str - cnt - m;
            }
        }
        if ( !*str && !*sstr )
            return str- cnt - m;
        else
            return -2;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Very probably you cannot optimize all 3 characteristics (code size, run speed, memory usage) at the same time: optimize any one of them at the expense of the other two is a good thing to hope for; optimize two at the expense of the other is the best you can obtain.

    To measure code size: learn assembly, make the compiler output assembler, compare
    To measure run speed: run and time each function a few million times, compare
    To measure memory usage: try valgrind
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    3
    bdb, I agree it's probably only possible to optimize one of those three at a time.
    But I wonder if it's necessary to output assembler code from the compiler to estimate executable code size. Isn't the size of the executable code in bytes sufficient to estimate RAM usage?
    Last edited by EEmaestro; September 24th, 2012 at 01:58 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by EEmaestro
    ... Isn't the size of the executable code in bytes sufficient to estimate RAM usage?
    Probably yes. I was thinking maybe the compiler can insert padding NUL instructions or debug symbols or some stuff that would make the sizes of the executables not directly comparable.
  8. #5
  9. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    code efficiency


    Use Static Code review tools such as CPPTest and Dynamic Code review tools such as Purify, memoryleakCatcher, etc.....or Keep a Review Panel Team of 4 One needs to Concentrate on Testability One needs to Concentrate on Completeness(Tracebility) One needs to Concentrate on Presentation ( Coding convention, naming convention, etc) One needs to Concentrate on Logics.


    [edit]Edited the deliberate link drop and permanently banned account. Evidently, the guy didn't read his two previous warnings about cut/pasting code.[/edit]

    Comments on this post

    • salem disagrees : dumb-*** copy/paste bot and link spammer (original here http://stackoverflow.com/questions/377104/efficient-code-review)
    Last edited by Scorpions4ever; September 25th, 2012 at 03:52 AM.
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    My sig has a writeup on performance programming. Instrumenting code to measure performance is a surprisingly difficult thing to do, getting reliable real-world information requires a great deal of experience and patient experimentation. In a surprising number of cases, it makes no nevermind in the real world and that time is better spent elsewhere.

    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

IMN logo majestic logo threadwatch logo seochat tools logo