We apply algebra not only for machine-, but also human understanding or "optimization" - nice if they are conform.Dann Corbit wrote: {General advice, not targeted at Dr. Hyatt, who already knows this well}:
The first rule of optimization is:
1. DON'T DO IT.
And the second rule (for experts only) is:
2. DON'T DO IT YET.
The general idea is that we should write code that is as clear and expressive as possible. Later on, when we want to make our program faster the right way to do it is to improve the algorithm. For instance, going from something that is O(N*N) to something that is O(N*log(N)) will create an enormous speedup and bit fiddling will never create anything more than a small linear factor.
So, when we have the code as clear and expressive as possible, we profile it. After we profile it, we speed up the hot spots. We can spend weeks making a slow routine faster only to discover that it is hardly ever called and so even though it is ten times faster than before the program shaves only one second off of a one hour run. A profile will tell you where to look to improve things.
Code: Select all
1) a*c + a*d + a*e
2) a * (c + d + e)
Recently I was quite surprised, that those expressions were identical:
Code: Select all
(1) a & ~b
(2) a & ( a ^ b )
(3) a ^ ( a & b )
(4) a - ( a & b )
(5) b ^ ( a | b )
(6) ( a | b ) - b
Code: Select all
BYTE islandsEastFiles(BYTE f) {return f & (f ^ (f >> 1));}
BYTE islandsEastFiles(BYTE f) {return f ^ (f & (f >> 1));}
BYTE islandsEastFiles(BYTE f) {return f & ~(f >> 1) ;}