Search code examples
cperformanceoptimizationprocessor

C Programming Optimization and Processor Extensions


I've got a C programming assignment which involves optimizing the code as much as possible. I've had a bit of a read about this on the Internet already and found things like using case over if, pass by pointers instead of value, etc.

What I'd like to ask about is why knowing what processor extensions are available, can help me to optimize my code? I know that SSE and AVX are available on the machine but what exactly does that mean to me as a programmer?

In relation my above question, I found the Intel Intrinsic Guide which I think is related to the processor extensions. Are there any advantages in terms of performance in using these functions over other C functions. e.g. would using _mm_sqrt_ps from xmmintrin.h be faster than sqrt from math.h?


Solution

  • The idea is to have per CPU optimized libraries (SSE, AVX etc), and call something like _may_i_use_cpu_feature() to dynamically determine what feature is available at runtime and load the "best" implementation for the CPU.

    For portable code you want to use sqrt() - and some runtime libraries have optimized implementation that are good enough. If you want full control and maximize performance on a specific platform, and don't care about portability, you can write hand optimized assembly (or use intrinsics).

    Most performance is gained by better algorithms anyways...