User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Which language?

Wed Jun 17, 2015 10:48 am

The designers of C++ have gone to a lot of trouble to ensure it can be implemented efficiently but that depends critically on the compiler writer. It is possible to generate fast efficient code but that may not always be done. It also relies on the user setting compile flags appropriately and maybe not just accepting the defaults.
Because most (all?) compilers inline templates they may make the program bigger but not neccessarily slower. I would not use the STL on an Arduino but on a pi why not if it is useful? There is no point keeping code size down unless there is a good use for the memory saved.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24971
Joined: Sat Jul 30, 2011 7:41 pm

Re: Which language?

Wed Jun 17, 2015 1:22 pm

RogerW wrote:The designers of C++ have gone to a lot of trouble to ensure it can be implemented efficiently but that depends critically on the compiler writer. It is possible to generate fast efficient code but that may not always be done. It also relies on the user setting compile flags appropriately and maybe not just accepting the defaults.
Because most (all?) compilers inline templates they may make the program bigger but not necessarily slower. I would not use the STL on an Arduino but on a pi why not if it is useful? There is no point keeping code size down unless there is a good use for the memory saved.
Also, code size is generally now dwarfed by the data size, so saving code to give more memory free for execution isn't necessarily worth it. So using templates, which can be effectively optimised for every different type they implement, is a worthwhile trade off for code size.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I own the world’s worst thesaurus. Not only is it awful, it’s awful."

Heater
Posts: 14453
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which language?

Wed Jun 17, 2015 5:45 pm

jamesh,

What I said was "Avoiding the STL is essential if you are writing code for small systems."

Perhaps I should have qualified "small". If you creating code for Arduino, Propeller, and other such micro-controllers you may only have 32K Bytes or so. Or perhaps you are writing for the multiple floating point processors in the Parallella Epiphany chip.

I think you will find it is impossible to use even one single feature from the STL without blowing more space than it is worth, if it's even possible to fit it in at all.

You will also often find that for such small systems there is a lot more code than data.
Memory in C++ is a leaky abstraction .

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24971
Joined: Sat Jul 30, 2011 7:41 pm

Re: Which language?

Wed Jun 17, 2015 6:39 pm

Heater wrote:jamesh,

What I said was "Avoiding the STL is essential if you are writing code for small systems."

Perhaps I should have qualified "small". If you creating code for Arduino, Propeller, and other such micro-controllers you may only have 32K Bytes or so. Or perhaps you are writing for the multiple floating point processors in the Parallella Epiphany chip.

I think you will find it is impossible to use even one single feature from the STL without blowing more space than it is worth, if it's even possible to fit it in at all.

You will also often find that for such small systems there is a lot more code than data.
32K isn't a 'small' system, it's a microscopic one! I'd be using C or assembler on something that small. C++ is almost always going to be too large unless you use it like C. Would be interesting to see how much space a simple vector via STL takes, vs writing it in C with the same level of functionality. I suspect similar.

Embedded systems I work on tend to have between 256MB and 1GB of RAM nowadays as it really is so cheap. They also run an RTOS or Linux. Never used an Arduino. Last machine I used with 32K was a BBC Micro., and 20k of that was for the frame buffer.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I own the world’s worst thesaurus. Not only is it awful, it’s awful."

Heater
Posts: 14453
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which language?

Wed Jun 17, 2015 6:58 pm

jamesh,

32K may be "microscopic" but there are still billions of microcontrollers being made and used that are far smaller than that. I agree C or assembler is ususally more appropriate there. Despite their small size C is often used.

At around the 32K size you may well find that you want the same code to operate on multiple sets of similar data. I.e. you want classes of objects. In C you would probably achieve that with structs and functions that take a pointer to a struct as a parameter. A kind of hand made class with methods.

Turns out that if you write that in C++ using statically allocated classes, the generated code can be exactly the same. All that happens is that C++ is hiding those instance pointers in the source for you. I was amazed to get that result the first time I did this.

So you are right, if you actually want or need the features that C++ offers it is very efficient at doing it and using C is not going to buy you anything for all the extra hassle of writing in it.

People have trouble when the expect to just be able to throw a "cout << ..." in there or use exceptions.
Memory in C++ is a leaky abstraction .

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Which language?

Mon Jun 22, 2015 9:24 pm

"New" programmers could start with groovy for an intro to the java world - a search for jvm languages throws up a ridiculous number of new and old languages... https://en.wikipedia.org/wiki/List_of_JVM_languages
Also llvm https://en.wikipedia.org/wiki/LLVM
You can drive yourself crazy looking for the right language....or the one that actually does what you want it to!!

RichR

Return to “General programming discussion”