Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 24th, 2012, 12:44 AM
 ccsr
Registered User

Join Date: Sep 2012
Posts: 13
Time spent in forums: 3 h 34 m 57 sec
Reputation 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);```

Quote:
 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
}```

Quote:
 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
September 24th, 2012, 04:26 AM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 33
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
September 24th, 2012, 02:56 PM
 EEmaestro
Contributing User

Join Date: Apr 2012
Posts: 59
Time spent in forums: 1 Day 18 h 13 m 7 sec
Reputation Power: 2
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 02:58 PM.

#4
September 24th, 2012, 03:05 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 33
Quote:
 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
September 25th, 2012, 02:11 AM
 andrewpattinson
Permanently Banned

Join Date: Aug 2012
Posts: 4
Time spent in forums: 51 m 7 sec
Warnings Level: 10
Number of bans: 1
Reputation 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]
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 04:52 AM.

#6
September 25th, 2012, 12:16 PM
 mitakeet
I'm Baaaaaaack!

Join Date: Jul 2003
Location: Maryland
Posts: 5,538
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 243
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.

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

 Viewing: Dev Shed Forums > Programming Languages > C Programming > How to compare code for efficiency