Shop OBEX P1 Docs P2 Docs Learn Events
C VS C++ why have a 10k diffrence in codesize ? — Parallax Forums

C VS C++ why have a 10k diffrence in codesize ?

AnubispodAnubispod Posts: 42
edited 2013-04-17 21:09 in Propeller 1
Hi i was playing around with a hello world demo and just added a float variable and the mathlib to it and did a atan . result compiled in C HUB LMM SPEED = 19.262bytes
and the almost the same in c++ toggle demo added a float do some math inc l atan , toggle led C++ HUB LMM SPEED = 2.232 bytes.

Why the huge size difference and what is the big downside from c++ then if its so small and tidy???

Best regards
Oliver. R

Comments

  • Heater.Heater. Posts: 21,230
    edited 2013-04-17 13:04
    I don't think this is a C vs C++ problem. Adding float maths will end up pulling in a huge lot of float math lib finctions which ever language. Try to do the same things in a .c program and tell us the difference.

    This subject has been debated a lot here and I know C++ will generate excactly the same sized code for similar functionality as C, even when introducing object oriented programming. Sorry I don't have time to find the links to those earlier threads just now. Of course if you start to use math and streams and such you pull in a lot of extra overhead.
  • Heater.Heater. Posts: 21,230
    edited 2013-04-17 13:07
    Sorry, am I reading your question backwards? Are you saying the C++ version has a smaller executable?

    I think you have to show us your code that demonstrates what you are talking about.
  • RaymanRayman Posts: 14,665
    edited 2013-04-17 13:32
    2kB sounds pretty tiny for a PropGCC program...

    I'd imaging C and C++ would be about the same size.
    I've been told that C++ only gets bloated when you add in the "standard library" for things like cin and cout, etc.
  • David BetzDavid Betz Posts: 14,516
    edited 2013-04-17 13:48
    Rayman wrote: »
    2kB sounds pretty tiny for a PropGCC program...

    I'd imaging C and C++ would be about the same size.
    I've been told that C++ only gets bloated when you add in the "standard library" for things like cin and cout, etc.
    You're right that 2K is too small. The LMM or CMM kernel is almost that big by itself. How are you determining the sizes of these programs? You can use propeller-elf-objdump -h to get the sizes of each of the program sections.
  • AnubispodAnubispod Posts: 42
    edited 2013-04-17 13:56
    When i compile and load the program it says how much on the left bottom side in the ide and in the log window.
    and here are the 2 samples i have ...
  • David BetzDavid Betz Posts: 14,516
    edited 2013-04-17 13:58
    Anubispod wrote: »
    When i compile and load the program it says how much on the left bottom side in the ide and in the log window.
    and here are the 2 samples i have ...
    Sorry, I am not that familiar with SimpleIDE and don't know how it determines program size. However, as I said, you can use propeller-elf-objdump -h to do that if you launch a command line window.
  • AnubispodAnubispod Posts: 42
    edited 2013-04-17 14:02
    Project Directory: C:/Users/R2D2/Documents/SimpleIDE/toggle/c++_toggle/ propeller-elf-c++ -o a.out -O2 -mlmm -I . -m32bit-doubles -fno-exceptions -fno-rtti toggle.cc -lm -lpthread
    propeller-elf-objdump -h a.out
    Done. Build Succeeded!
    propeller-load.exe -I C:/propgcc/propeller-load/ -b HUB -p COM7 a.out -e -r
    Propeller Version 1 on COM7
    Loading a.out to EEPROM via hub memory
    2232 bytes sent
    Verifying RAM ...
    OK
    OK
  • David BetzDavid Betz Posts: 14,516
    edited 2013-04-17 14:11
    Anubispod wrote: »
    Project Directory: C:/Users/R2D2/Documents/SimpleIDE/toggle/c++_toggle/ propeller-elf-c++ -o a.out -O2 -mlmm -I . -m32bit-doubles -fno-exceptions -fno-rtti toggle.cc -lm -lpthread
    propeller-elf-objdump -h a.out
    Done. Build Succeeded!
    propeller-load.exe -I C:/propgcc/propeller-load/ -b HUB -p COM7 a.out -e -r
    Propeller Version 1 on COM7
    Loading a.out to EEPROM via hub memory
    2232 bytes sent
    Verifying RAM ...
    OK
    OK
    That's very odd. And you're saying that the resulting program works? What is in toggle.cc? SimpleIDE has different sample programs than what are in the propgcc project so I want to make sure that I'm looking at the right source.
    Edit: Can you send me the a.out file?
  • David BetzDavid Betz Posts: 14,516
    edited 2013-04-17 14:19
    I just looked at the versions of these that are in propgcc/demos/toggle and they are both very small. I think the size you are seeing for the C++ version seems reasonable. I'm not sure why the C version would be so much bigger.
  • AnubispodAnubispod Posts: 42
    edited 2013-04-17 14:32
    its the printf what makes it explode in size... that was the difference
    fom 2.2k to 19k wow
  • David BetzDavid Betz Posts: 14,516
    edited 2013-04-17 14:44
    Anubispod wrote: »
    its the printf what makes it explode in size... that was the difference
    fom 2.2k to 19k wow
    Yes, that would certainly do it! Glad you figured it out.
  • User NameUser Name Posts: 1,451
    edited 2013-04-17 14:55
    I was using sprintf just do base conversions. With that one function accounting for 70% of the total compiled code, I was compelled to write my own routine. It turned out to be 1/10 the size and ran 3x faster. Subsequent experience suggests this is true across a broad ranges of processors and compilers. Printf is a whale in the china shop.
  • David BetzDavid Betz Posts: 14,516
    edited 2013-04-17 15:03
    User Name wrote: »
    I was using sprintf just do base conversions. With that one function accounting for 70% of the total compiled code, I was compelled to write my own routine. It turned out to be 1/10 the size and ran 3x faster. Subsequent experience suggests this is true across a broad ranges of processors and compilers. Printf is a whale in the china shop.
    Yeah, but it sure is handy! :-)
  • jazzedjazzed Posts: 11,803
    edited 2013-04-17 15:16
    Use the tiny library. Add this to Project Options -> Other Linker Options box: -ltiny
  • User NameUser Name Posts: 1,451
    edited 2013-04-17 15:32
    David Betz wrote: »
    Yeah, but it sure is handy! :-)

    I totally agree. It's uncommonly flexible and useful.

    BTW, I wish I'd said something like... On a memory-limited uC it's like sharing a twin bed with a Saint Bernard.
  • RaymanRayman Posts: 14,665
    edited 2013-04-17 16:09
    i wonder how much of printf counts as code in xmmc mode...
    most I imagine...
    then, it's just a drop in a big pool...
  • SRLMSRLM Posts: 5,045
    edited 2013-04-17 21:09
    Personally, I use printf for quick debugging and sending data to the terminal in applications where speed/size doesn't matter. For my core applications, I use a port of FFDS1 for the serial.
Sign In or Register to comment.