Arduino Library Timings

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. :slight_smile:

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)

The ATmega328P and ATmega32U4 are microcontrollers, you should start using the 32-bit Due or a Zero (or other M0).
At Adafruit they have a Feather board with a M0 : Adafruit Feather M0 Basic Proto - ATSAMD21 Cortex M0 : ID 2772 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits

Both a integer and float divide takes more time than multiply. That is a known fact for the AVR chips.

Using a byte instead of an int is faster of course on a 8-bit microcontroller. I'm not sure about the others, you need to provide a test-sketch to prove it.

The compiler can do all sorts of optimizations. By changing a little thing, it might produce completely other code. So it very hard to say "this or that" will improve the speed.
It is possible to add custom compiler options to platform.local.txt. The Arduino optimizes default for size, so adding -Os will guaranteed make it faster. Also the #pragma can sometimes be used with a different compiler option, but that is for a single file, and I don't know how the preprocessor handles that.

Case Switch

A case statement in gcc will compile to code similar to a series of if/else if statements (where the time to execute a case will be dependent on where that case falls in the statement), or a jump table (which has more setup, but has constant time for all cases, and is faster if there are a lot of densely packed cases.)

in PIC's it's a lot easier/faster to find out if longX = longY than to find out if longX>longY.

In C? I dunno. Four compares vs four subtract instructions; I think they're about equal timing on the AVR (which has more direct multi-precision math than the simpler PICs.)

In any case, these are pretty subtle optimizations - if you care about execution speed at this level, you should learn to disassemble the compiler output an look at and understand the resulting assembly language. It's no long a case of "digitalWrite() takes nearly 30x longer than a direct port write" where the applicable conditions and the performance are both really obvious.

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.

Well, then, you should, because such a blanket statement is nonsense. Adding the overhead of a function call will NOT make code faster.

I suspect it may be to do with relative jumps vs. tabling?

Like jumping to ridiculous conclusions vs. a table of facts?

I agree with @PaulS.

Post the code that demonstrates the points you are making.

Switch/Case and comparisons with > and < have nothing to do with Arduino libraries.

...R