I generally code in C. sometimes have to use C++.
Experience helps to write code that converts to efficient machine codes.
Algorithm with asymptotically faster complexity is not the final solution,
its implementation also matters.
For example, if we want to find " j= i mod 32 "
[c] j = i & 0x1F; [/c]
should be bettter than
[c]j = i % 32; [/c]
Surely this is a mathematical trick. There are also language related tricks.
Like, doing type casting inside big loop should be a bad practice because
casting is a time consuming process.
Can anyone give some tips/tricks about writing codes that increases the efficiency of the final machine code.
If there is some books/web sites known to you on this matter, please post its name here/ send me a private message/mail me to hellopopel@hotmail.com
Writing Optimized Codes/ tricks
Moderator: Board moderators
Writing Optimized Codes/ tricks
μδ. ταηνιπ αλ αμιη
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
IMHO you should leave these tricks to the compiler, it will optimise expressions like i%32, 8*i, etc. by using the appropriate rotate machine instructions. i&0x1f is wrong if i<0, so mindlessly using these 'tricks' will lead to bugs.
Only when you realy know what you're doing, and you're absolutely certain that it generates faster machine code, and you realy need the extra microsecond, you should use 'groovy' code. Compilers are a lot smarter than 'smart' programmers, nowadays...
My favourite book on this subject is Jon Louis Bentley's "Writing Efficient Programs". It contains a lot of tips to optimise programs, without maiming them to unreadable.
Only when you realy know what you're doing, and you're absolutely certain that it generates faster machine code, and you realy need the extra microsecond, you should use 'groovy' code. Compilers are a lot smarter than 'smart' programmers, nowadays...
My favourite book on this subject is Jon Louis Bentley's "Writing Efficient Programs". It contains a lot of tips to optimise programs, without maiming them to unreadable.