Thread: Output size

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    Output size

    Hello,

    My application is designed mainly for speed, so I have set all compiler options for optimize for speed. Given this, what are the ways I can use to reduce output size without lower speed? (I am thinking mainly in design optimizations or something like that, is it possible?)

    thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The doctor must see the patient to make a correct diagnosis!

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by Adak View Post
    The doctor must see the patient to make a correct diagnosis!
    Well, i was thinking in generic things and not specifics, i.e. I dont know if having big int arrays ( int [] ) increase output size.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    One can only guess at the OS/Compiler you're using...

    > i.e. I dont know if having big int arrays ( int [] ) increase output size.
    Generally speaking, if you initialise global data (in program data initialisations, then it will take up space in the executable image loaded from storage.
    Eg.
    Code:
    int a[1000];  // probably goes in bss section
    
    int a[1000] = { 0 };  // probably goes in data section
    But then you have to make sure you initialise it at runtime.

    > My application is designed mainly for speed, so I have set all compiler options for optimize for speed.
    But unless your application takes well below 1 second of run-time, the amount of time to load the program from disk is going to be pretty minimal.
    Saving 50mS in an hour doesn't count as an improvement - it's noise.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Here are some suggestions:
    - Use constants instead of #define's.
    - Create constants for commonly used strings that are identical in several locations.
    - Read large arrays from a file instead of hard-coding the data in the code.

    Unless your program is written really badly, you might be able to save 1-2 KB. Is it worth your time?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by cpjust View Post
    - Use constants instead of #define's.
    What is the reason for this??? I have lot of #defs....

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Kempelen View Post
    What is the reason for this??? I have lot of #defs....
    Because macros just copy and paste data all over the place, so you have multiple copies of the same data everywhere. Constants are only stored in one place.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094

    Talking

    Quote Originally Posted by cpjust View Post
    Because macros just copy and paste data all over the place, so you have multiple copies of the same data everywhere. Constants are only stored in one place.
    AHAM! VEWWWY important C knowledge. Maybe in Java or C# you can afford to screw around with memory allocations, but not in C my friend!

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by cpjust View Post
    Because macros just copy and paste data all over the place, so you have multiple copies of the same data everywhere. Constants are only stored in one place.
    I dont see it. Macros are not data but machine instructions... or are you refering to macros which has a data declaration in it¿¿

    Also I think is the same to use:
    #define myint 10
    than declare :
    const int myint 10;

    I dont see how this sample could be different in size.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Kempelen View Post
    I dont see it. Macros are not data but machine instructions... or are you refering to macros which has a data declaration in it¿¿

    Also I think is the same to use:
    #define myint 10
    than declare :
    const int myint 10;

    I dont see how this sample could be different in size.
    Try these and you'll probably see a size difference:

    Code:
    #include <stdio.h>
    
    #define str "blah, blah, blah..."
    
    int main()
    {
       printf( str );
       printf( str );
       printf( str );
       return 0;
    }
    Then replace the #define line with:
    Code:
    const char* str = "blah, blah, blah...";
    and recompile.
    You might need to turn off optimizations in your compiler.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    *shrug*
    The OP still hasn't answered what platform they're using, or the lifetime of the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by Salem View Post
    *shrug*
    The OP still hasn't answered what platform they're using, or the lifetime of the program.
    mingw in win32

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About output size
    By Kempelen in forum C Programming
    Replies: 1
    Last Post: 11-24-2009, 07:43 AM
  2. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  3. Stack around the variable 'Output' was corrupted
    By jacksb68 in forum C Programming
    Replies: 5
    Last Post: 09-08-2009, 08:55 AM
  4. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  5. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM

Tags for this Thread