Hi team,
Out of curiosity, I did some comparison on using C and C++ on a low end MCU. The MCU I use is MSP430G2333 (4K Flash, 256B ram). Compiler version: TI v18.1.6.LTS, all default settings, IDE code composer v8.3.1
I found that the code generated with C++ is a little less than the C.
Is this a fair comparison?
Here is the codes I used for testing:
Out of curiosity, I did some comparison on using C and C++ on a low end MCU. The MCU I use is MSP430G2333 (4K Flash, 256B ram). Compiler version: TI v18.1.6.LTS, all default settings, IDE code composer v8.3.1
I found that the code generated with C++ is a little less than the C.
- C, 80B RAM, 84B Flash
- C++, 80B RAM, 88B Flash
Is this a fair comparison?
Here is the codes I used for testing:
Code:
#include <msp430.h>
class Test{
public:
void hello(){
unsigned long int i = 0xFFFF;
unsigned long int result = 0;
P1DIR |= BIT0;
while(i--){
WDTCTL = WDTPW | WDTHOLD; // test register access
result = result + i; // simple calculation
P1OUT ^= BIT0; // toggle a LED
}
}
Test(){
// nothing
}
~Test(){
// nothing
}
private:
// nothing
};
void hello(){
unsigned long int i = 0xFFFF;
unsigned long int result = 0;
P1DIR |= BIT0;
while(i--){
WDTCTL = WDTPW | WDTHOLD; // test register access
result = result + i; // simple calculation
P1OUT ^= BIT0; // toggle a LED
}
}
/**
* main.c
*/
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
while(1){
/* comment out hello() or Test.hello() to test different language */
hello();
//Test test;
//test.hello();
}
}