Jump to content
43oh

Recommended Posts

Well I was just talking with Rick via IRC, and showed him my C implementation, where he mentioned that C++ will aways try to inline functions where C has to be told.

 

So after adding inline to the C implementation . . .

-------------- Build: Debug in test (compiler: GNU GCC Compiler for MSP430)---------------

msp430-gcc.exe   -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=msp430g2553    -c main.c -o obj\Debug\main.o
msp430-gcc.exe  -o bin\Debug\test.elf obj\Debug\main.o   -Os -Wl,-gc-sections,-u,main -mmcu=msp430g2553  
Output size is 9.21 KB
Running target post-build steps
C:\HighTec\msp430\bin\msp430-size D:\projects\Ctest\bin\Debug\test.elf
   text	   data	    bss	    dec	    hex	filename
    216	      0	      2	    218	     da	D:\projects\Ctest\bin\Debug\test.elf
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings (0 minutes, 0 seconds)
 

 

The code compiles down to the same exact size. That is for a single print statement of "hello world\r\n".

 

@@chicken, I never use ram unless absolutely necessary. I prefer to leave it for the calling stack.

Link to post
Share on other sites
  • Replies 32
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Point is, you're the only one arguing about what is best, and what is not.  My whole stance has been ( going back what now ? 2-3 months ). Be flexible, and use what makes sense.  Of course, if your ex

Again . . .    Being a bad programmer makes you a bad programmer. You and Linus can both agree all you two want, but that does not make either of you right.   C can not do function overloading . .

I think you'll find, at least with the "original" digital guys ( who are still around ) assembly is the real go to language. I find that most of these guys who truly know their stuff, know assembly ve

So for a "reasonable" use case, these 6 PWM drivers will use about 150 bytes of flash and 20 bytes of RAM. On the C side, I could fix this stuff with only 50 bytes of flash and 12 bytes of RAM.

It would be interesting to see this fleshed out in 'C' so some real comparisons can be made.

 

-rick

Link to post
Share on other sites

More testing . . . I was *thinking* that with multiple calls to the print methods of each of these implementations that C would take a slight lead. But I was wrong. C++ actually comes in much smaller. However, because the difference is once again fairly big, I suspect there may be some optimization that can be done here.

 

Each implementation calls print->string method 5 times( two calls are "\r\n" ), and print->number 2 times. Test again are using the GNU toolchain.

 

C implementation:

 

-------------- Build: Debug in test (compiler: GNU GCC Compiler for MSP430)---------------

msp430-gcc.exe   -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=msp430g2553    -c main.c -o obj\Debug\main.o
msp430-gcc.exe  -o bin\Debug\test.elf obj\Debug\main.o   -Os -Wl,-gc-sections,-u,main -mmcu=msp430g2553  
Output size is 11.04 KB
Running target post-build steps
C:\HighTec\msp430\bin\msp430-size D:\projects\Ctest\bin\Debug\test.elf
   text	   data	    bss	    dec	    hex	filename
    600	      0	      2	    602	    25a	D:\projects\Ctest\bin\Debug\test.elf
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings (0 minutes, 1 seconds)

 

C++ implementation.

 

-------------- Build: Debug in msp430 (compiler: GNU GCC Compiler for MSP430)---------------

msp430-g++.exe   -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=msp430g2553    -c main.cpp -o obj\Debug\main.o
msp430-gcc.exe  -o bin\Debug\msp430.elf obj\Debug\main.o   -Os -Wl,-gc-sections,-u,main -mmcu=msp430g2553  
Output size is 9.99 KB
Running project post-build steps
C:\HighTec\msp430\bin\msp430-size D:\projects\msp430\bin\Debug\msp430.elf
   text	   data	    bss	    dec	    hex	filename
    440	      0	      2	    442	    1ba	D:\projects\msp430\bin\Debug\msp430.elf
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings (0 minutes, 1 seconds)

 

The difference is as you can see exactly 160 bytes on target( estimated ). What exactly needs to be done to bring the difference down on the C implementation, I am not sure, but to be honest I really do not care. At this point, I am tired of baby sitting C, and would use C++ just for less hassle. With that said though, there is nothing stopping me from writing the code as C, and compiling it as C++( naming the file *.cpp may be enough for the compiler to handle most things ). We'll see, I will test that in a bit.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...