Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are you favorite low level code optimization tricks? [closed]

Tags:

optimization

I know that you should only optimize things when it is deemed necessary. But, if it is deemed necessary, what are your favorite low level (as opposed to algorithmic level) optimization tricks.

For example: loop unrolling.

like image 639
Himadri Choudhury Avatar asked Feb 27 '09 02:02

Himadri Choudhury


People also ask

What is code optimization with example?

Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed. In optimization, high-level general programming constructs are replaced by very efficient low-level programming codes.

What is the importance of optimizing a code?

Code optimization is any method of code modification to improve code quality and efficiency. A program may be optimized so that it becomes a smaller size, consumes less memory, executes more rapidly, or performs fewer input/output operations.


1 Answers

++i can be faster than i++, because it avoids creating a temporary.

Whether this still holds for modern C/C++/Java/C# compilers, I don't know. It might well be different for user-defined types with overloaded operators, whereas in the case of simple integers it probably doesn't matter.

But I've come to like the syntax... it reads like "increment i" which is a sensible order.

like image 198
Thomas Avatar answered Sep 19 '22 13:09

Thomas