Discuss How to compare code for efficiency in the C Programming forum on Dev Shed. How to compare code for efficiency C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
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 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);
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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
Posts: 58
Time spent in forums: 1 Day 17 h 52 m 2 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 01:58 PM.
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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.
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.
[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]
Last edited by Scorpions4ever : September 25th, 2012 at 03:52 AM.
Posts: 5,538
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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.
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