Hello all,
I have noticed, even though I've only been into Arduino for 2 years, that some functions take longer than others.
Most of us know that direct port manipulation is way faster than digitalRead/Write, and there's better ways to handle the ADC, but I have suspicions about other library items:
Case Switch:
I have noticed an increase in the time it takes for a Case Switch to execute. It seems the more stuff that's between Case and Break, the slower it runs. I haven't done speed tests with specific items, but I have noticed that just inserting a call to a function (void whatever()) makes it run faster.
It's a bit of a pain though to create a function for every little thing that may be done inside a Case.
I suspect it may be to do with relative jumps vs. tabling?
Using > or < with longs:
After almost 2 decades of programming experience with PIC microcontrollers, I understand that dealing with longs takes a lot of code. Once again, just musing, but in PIC's it's a lot easier/faster to find out if longX = longY than to find out if longX>longY. Comparing 2 sets of 4 bytes would mean subtracting, carries, and equal tests.
Using bit logic in place of > and <
Not so sure about this one, but say you write:
if (x>127)
This should be more efficient as:
if(x & 0x80)
or even better:
if(x>3) x=0;
being replaced by
x&=0x03;
Using Byte instead of Int wherever possible:
I'm pretty sure about this one. If counting a loop or whatever only requires <256 and no negative numbers, making the variable a byte should speed things up. Unsigned/Signed Int uses two bytes, thus taking twice as long. This is all in theory but it makes sense. It saves RAM as well.
The only drawback is if it is going to be converted to a signed int, care must be taken.
There should be some sort of standard bunch of timings listed, and test results, compiled into a single page or topic. This would be a huge time saver for everyone.
I'd be willing to post such a list on my site as well, unless there's already something out there that has escaped me on google.
I could test many of these myself, but there's such a miriad of other things happening in the Arduino setup, I don't know if those results can be trusted in all situations. I don't know the Atmel chips at that level yet (compared to PIC is like comparing Apple to Microsoft, totally different methods in datasheet, totally different everything)