### Thread: How to compare code for efficiency

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 substr1[]="ggghij";
char substr2[]="ggghij";
char substr3[]="ggghijk";
char substr4[]="ggghijj";
char substr5[]="xyyyyzzzz"
char substr6[]="ccdex"

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. No Profile Picture
bdb
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
3. 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.
4. No Profile Picture
bdb
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.
5. 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.

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