Thought Leadership

Optimal code

Hands up who wants to write optimal code? If your hand is not up, it means that either you are not paying attention or are not a software developer. Nobody wants to write sub-optimal code, do they? However, this does raise some questions. What exactly does “optimal” mean and how do you go about attaining it? …

As with many value judgements, optimal can mean different things to different people. As a computer user, I would say that optimal code in that context is fast and reliable. Speed is much more important than size [within reason]. Memory is plentiful, so size only really matters because it can affect start up time. I just want to get on with stuff. Obviously everyone wants reliable software.

With embedded applications, the priorities may be different. With many devices speed is important, but may not be overwhelmingly so. Size of code code can be critical with limited resources available. Power consumption can be very important, particularly with battery-powered devices. This adds a third dimension to the optimization issue. Again, everyone want embedded software to be reliable.

So, how do we get to optimal in a given context?

Speed and size: To get the fastest or smallest code [or the right balance between these two, sometimes opposing, forces] is down to compiler optimization. Embedded compilers tend to have extensive control over optimization. It is almost always fruitless to try to write faster/smaller code in C; it is better to let the compiler do its job. Sometimes, but quite rarely, more optimal code may be written in assembly language, but this comes at the cost of maintainability, portability and reliability.

Power: There are a number of factors that can affect power consumption. From the software perspective, fast code will get more work done in a given number of cycles, which enables a lower power CPU to be used. However, fast code is commonly larger, which necessitates more memory that will consume power. A balance is needed. Power demands are dynamic and vary with the task being performed. Managing power consumption is best achieved by using an RTOS with a power management framework.

Reliability: If you use good quality development tools, you should be able to assume that they produce reliable output – it simply depends on the source code that they process. So, writing reliable code is the developer’s responsibility. This can be achieved by always aiming to write code in a simple, readable, maintainable fashion. If you need guidance, then follow a methodology aimed at helping produce reliable code, such as MISRA C.

The bottom line is that nobody can say that some code is optimal in an absolute sense. Steps may be taken to make it optimal for a specific application or context.

Colin Walls

I have over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, I am a member of the marketing team of the Mentor Graphics Embedded Systems Division, and am based in the UK. Away from work, I have a wide range of interests including photography and trying to point my two daughters in the right direction in life. Learn more about Colin, including his go-to karaoke song and the best parts of being British: http://go.mentor.com/3_acv

More from this author

Leave a Reply

This article first appeared on the Siemens Digital Industries Software blog at https://blogs.sw.siemens.com/embedded-software/2020/05/18/optimal-code/