C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 23rd, 2012, 11:44 PM
ccsr ccsr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 13 ccsr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);


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;
}

Reply With Quote
  #2  
Old September 24th, 2012, 03:26 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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

Reply With Quote
  #3  
Old September 24th, 2012, 01:56 PM
EEmaestro EEmaestro is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 58 EEmaestro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old September 24th, 2012, 02:05 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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.

Reply With Quote
  #5  
Old September 25th, 2012, 01:11 AM
andrewpattinson andrewpattinson is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 4 andrewpattinson Negative: is most likely a SPAMMER and a traitor to the cause. 
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]
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.

Reply With Quote
  #6  
Old September 25th, 2012, 11:16 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
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.
__________________

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to compare code for efficiency

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap