I've just added Maik Schmidt's Arduino: A Quick Start Guide to my expanding library. Overall, I like the book and think it's well-written. (I've written 15 programming books and taught in the computer science department at a Big Ten university so I'm comfortable commenting on writing style.) However, there are a few places where he is misleading and perhaps even wrong.
On page 94, he makes the statement:
So, whenever you define a constant value in your program, declare it as such (using const, not using #define).
Blanket statements like this are always dangerous and #define has a place in C. First, a #define is a textual replacement...that's all. The result is that there is no entry in the symbol table...there is no rvalue or lvalue. So what, you might ask. Well, it has two impacts on your Arduino code. First, #define's are resolved at compile time, not run time. Second, when coupled with the first fact suggests that the code should run slightly faster for #define's since there is no table look up. I wrote a small test program and found that, in a tight loop, there is about a 3.5% hit on performance when using const versus #define. On the downside, because #define's are textual replacements and they are not in the symbol table, they cannot be examined by a debugger...they're gone after compilation. However, since they are constants, this is no big loss.
Another thing to keep in mind is that about the slowest basic math thing you can do is a division. At one place in the book, he shows the code:
return (voltage * 1000 - 500) / 10;
This can be rewritten as:
return (voltage * 1000 - 500) * .10; // Original was divided by 10
In a tight loop, this change saved about 3% execution time. If you're working with integers, you can also do bit shifting to divide by 10. If you do something weird like this, make sure you comment what you did. (I didn't time bit shifting, but my guess is that it would be even faster.)
For what it's worth, I did the multiply versus divide test on Visual Studio using C# and found an almost a 66% speed improvement using the multiply on floating point numbers. If you work in VS, forget the float data type. Because of the builtin numeric cooprocessor (e.g., built upon the old 8087 chip) in the Pentium class chips, all floats get promoted to double, the operation is then calculated, and then the result must be "demoted" back to a float for the answer. All this promotion-demotion takes time. The #define versus const tradeoff in VS is virtually the same as the Arduino results.
Finally, when I started the test on the Arduino IDE, I was stunned at the speed of the loop. It was so fast that I tripled the number of loop passes and ran the test again. No change in the time. Then I had a flat-forehead epiphany (you know, where you ram the heel of your hand into your forehead wonder how you could be so dumb). Because I did nothing with the result of the single statement math calculation in the loop, the compiler simply ignored the loop! This suggested a pretty code code parser, so I checked to see where the Arduino compiler came from. I then learned it's based on the Gnu (Open Source) compiler, which is a pretty darn good compiler.
The lessons to be learned: 1) #define's are not bad things and actually give a slight performance boost, 2) when working with floating point numbers and constants, use multiplication instead of division for constants whenever you can, and 3) resolve any constants that you can rather than doing it in code. For example, a temperature conversion:
F = C x 9/5 + 32
is faster if rewritten:
F = C x 1.8 + 32 // 1.8 = 9/5
While this change won't make a hill of beans difference alone, it could if it's in a tight loop.
I hope this helps...