C / C ++ Optimization

I deal exclusively with OO when it comes to programming, which, unfortunately, means that highly optimized code is not my forte. I am pretty good at C and usually do it reasonably prudently, but it's still hard for me to think about the best way to handle situations.

One example would be:

int strlen(const char* str) { char* s; for (s=str; *s; ++s); return s-str; } 

I would never have thought of it myself.

So what are the good resources that optimized code provides you with? I would like to find a place where I could read the theory behind it, which makes the compiler in the background, which makes it useful, etc.

It would be nice if some resources were noted for studying optimized data structures using real-world scenarios, but this is probably too much to ask.

+6
source share
2 answers

Do not try too hard with micro-optimization. With modern day compilers, it is best to optimize how those handled by the compiler. It is better to spend your time choosing the right algorithms and design patterns for your applications. Find a decent profiler and learn how to use it. Don't waste time figuring out how to optimize strlen .

As for the links on how to do these micro optimizations, I already mentioned this before, but I will do it again with pleasure, the Agner manual is simply outstanding and free :) Check out: http://www.agner.org/optimize/ should be many manuals.

BTW: The most optimized version of strlen that I came across was made by Agner: http://www.agner.org/optimize/asmlib-instructions.pdf and written in the assembly ;; -)

+5
source

What every programmer should know about memory, part 1 is one resource that was interesting. He gets into many silly low-level details.

However, one of the main points of optimization is the size of the code. If you use intelligent OO design, small design, you optimize. I personally believe that ignoring small details and focusing on a high level will help you in the future and in your career. In addition, gprof is better at this than ever.

+1
source

Source: https://habr.com/ru/post/893808/


All Articles