Monday, May 3, 2021

Embedded C Interview questions

MORE IMPORTANT QUESTIONShttp://pandyaelectronics.blogspot.com/2021/06/an-ultimate-guide-for-embeddedfirmware.html


 https://sites.google.com/site/embedsystem1/home/interview-questions

https://www.codeproject.com/Articles/6154/Writing-Efficient-C-and-C-Code-Optimization

  • Count-down loops is always better than count-up loops (primarily because, != is faster than <= operation)
  • Loop Unrolling: Small loops can be unrolled for higher performance, with the disadvantage of increased code size.
  • Referring to address of local variable results in unoptimized code: Reason - Compiler will try to optimize by using it's registers for the variables; but when you refer the address, it cannot allocate a register and rather use memory (and there-by resulting in memory operation)
  • Reduce global variables: For the same reason that it has to use memory.
  • char, short, int - which is better?: Use int as much as possible because for 'char' and 'short' CPU has to do some shift operations before giving the value to the user
  • How to reduce the function call overhead?

  • Avoid using ++ and -- etc. within loop expressions. E.g.: while(n--){}, as this can sometimes be harder to optimize.
  • Minimize the use of global variables.
  • Declare anything within a file (external to functions) as static, unless it is intended to be global.
  • Use word-size variables if you can, as the machine can work with these better (instead of charshortdouble, bit fields etc.).
  • Don't use recursion. Recursion can be very elegant and neat, but creates many more function calls which can become a large overhead.
  • Avoid the sqrt() square root function in loops - calculating square roots is very CPU intensive.
  • Single dimension arrays are faster than multi-dimension arrays.
  • Compilers can often optimize a whole file - avoid splitting off closely related functions into separate files, the compiler will do better if it can see both of them together (it might be able to inline the code, for example).
  • Single precision math may be faster than double precision - there is often a compiler switch for this.
  • Floating point multiplication is often faster than division - use val * 0.5 instead of val / 2.0.
  • Addition is quicker than multiplication - use val + val + val instead of val * 3puts() is quicker than printf(), although less flexible.
  • Use #defined macros instead of commonly used tiny functions - sometimes the bulk of CPU usage can be tracked down to a small external function being called thousands of times in a tight loop. Replacing it with a macro to perform the same job will remove the overhead of all those function calls, and allow the compiler to be more aggressive in its optimization..
  • Binary/unformatted file access is faster than formatted access, as the machine does not have to convert between human-readable ASCII and machine-readable binary. If you don't actually need to read the data in a file yourself, consider making it a binary file.
  • If your library supports the mallopt() function (for controlling malloc), use it. The MAXFAST setting can make significant improvements to code that does a lot of malloc work. If a particular structure is created/destroyed many times a second, try setting the mallopt options to work best with that size.

Last, but definitely not least - turn compiler optimization on! Seems obvious, but is often forgotten in that last minute rush to get the product out on time. The compiler will be able to optimize at a much lower level than can be done in the source code, and perform optimizations specific to the target processor.

No comments:

Post a Comment