Single Parameter vs Struct:  Performance in Embedded Systems

Single Parameter vs Struct: Performance in Embedded Systems

In my previous post, I explored the performance benefits of using parameters instead of global variables to reduce cycle time ⌚ (link in the first comment). Building on that, I wanted to dig deeper and examine a new angle: is it more efficient to pass data to a function via structs rather than as individual parameters?

Function Examples:

1. Using single parameters

Here, I used three separate parameters for the calculation. Although passing Sum may seem redundant, including it keeps the comparison fair, as Sum is also part of the struct in the next example.

Sum = CalcSumWithThreeParam(Num1, Num2, Sum);        

2. Using one struct

In the second approach, I used a struct to pass the same values, as defined below:

SysTick_StrLocSumThreeElem.Sum = CalcSumWithStr(SysTick_StrLocSumThreeElem);        
typedef struct
{
    int Sum;
    int Num1;
    int Num2;
} SysTick_StrSumCalcThreeElem;        

Experiment Setup

The experiment mirrored the setup from my global vs. local parameters test. I created two struct sizes: one with three elements and a larger one with seven elements. I then measured the cycle counts required to execute each function call.

With 3 elements in the struct


Article content

With 7 elements in the struct


Article content

Results 🥅

Scenario 3 Elements

  • Single Parameters [Cycles]: 50 🥈
  • Struct [Cycles] : 49 🥇

Scenario 7 Elements

  • Single Parameters [Cycles]: 84 🥇
  • Struct [Cycles] : 85 🥈


Assembly Code Analysis: An Interesting Discovery 🔬

When analyzing the assembly code, I noticed a distinct approach for structs. The assembler uses the stm (Store Multiple) instructions to save all elements of a struct in consecutive memory blocks. This contrasts with individual parameters, where each requires a separate str (Store) instruction.


Article content

My Surprise:

Based on the stm instruction, structs might seem more efficient since stm allows storing multiple values at once. However, the experiment results showed no clear performance gain.

Why Is This the Case? ⁉️

After further research, I found an explanation: str (store) instructions can actually be faster on a cycle-per-instruction basis than stm. While stm reduces the instruction count by packing multiple stores into one, str instructions, when used individually, can sometimes be quicker due to the hardware’s ability to pipeline them efficiently.

Conclusion 🎁

While structs bring clear code readability and alignment benefits, the performance advantage in cycle time remains minimal, especially for small-sized data. If you’re optimizing for cycle time specifically, individual parameters may perform just as well as structs.

Based on feedback from previous discussions with Eduard Drusa , it’s clear that modern compilers are highly optimized and often make the best performance choices automatically. In many cases, compilers streamline the code to achieve optimal cycle times, reducing the need to manually fine-tune performance.

➡️ Takeaway: Leveraging the compiler’s advanced optimizations can save time, allowing developers to focus on higher-level architecture rather than hand-tuning each instruction.

Have you encountered any cases where structs provided a performance boost? I’d love to hear about your experience!

wassim zaier

Electrical Engineer with a focus on embedded systems

5mo

Do you use timestamp counters here for benchmarking to measure clock cycles?

Like
Reply
Eduard Drusa

CMRX memory protected realtime microkernel for MCUs | Software is not a crankshaft

6mo

This is still wrong on so many levels. The readability benefits of using structs are questionable, or better said - context sensitive. Sometimes it is better to use structs, if they logically group things together. Sometimes it is not. Performance would be very last on my list of concerns. And if I ever saw anyone assessing performance of this, I'd be like: "They are scraping the very bottom of the barrel" - thought Stierlitz

Madonna Magdy Moussa

Embedded Software Engineer

6mo

Informative 👌🤸♀️

To view or add a comment, sign in

More articles by Alexander Koenig

  • Embedded Software Architecture 🏣

    Embedded Software Architecture 🏣 𝗥𝗲𝗰𝗮𝗽 𝗼𝗳 𝘁𝗵𝗲 𝗹𝗮𝘀𝘁 𝗽𝗼𝘀𝘁 𝗼𝗻 Static view in 𝗘𝗺𝗯𝗲𝗱𝗱𝗲𝗱…

Insights from the community

Others also viewed

Explore topics