# Speed of floating point operations -- test results

Was curious as to the speed of floating point operations on the 16MHz Arduino Diecimila. Could only find a few threads about that (e.g., http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206534749), with no answer, so I ran some tests. Here's a graph of results comparing the speed of a multiply operation using the various data types:

The way the C test program was set up, the values above include the time for the multiply operation as well as one memory read and one memory write.

These are the lengths of the data types in bytes:

byte 1
int 2
long int 4
double 4

I also looked at the time to do a sin() call to the math library: 255 microseconds!

Anyway, based on these results, looks like the Diecimila is about a 0.1 megaflop device, using 4-byte floats/doubles.

These are the lengths of the data types in bytes:

byte 1
int 2
long int 4
double 4

...

Anyway, based on these results, looks like the Diecimila is about a 0.1 megaflop device, using 4-byte floats/doubles.

I'm too lazy to check for myself, but won't a `float` be 4 bytes and a `double` be 8 bytes? (Referring to: Fundamental types - cppreference.com)

--Phil.

I'm too lazy to check for myself, but won't a `float` be 4 bytes and a `double` be 8 bytes? (Referring to: Fundamental types - cppreference.com)

Not for the Arduino--sizeof(float) and sizeof(double) both return 4. As your link states: "The size and range of any data type is compiler and architecture dependent."

Joe

Was curious as to the speed of floating point operations on the 16MHz Arduino Diecimila. Could only find a few threads about that (e.g., http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206534749), with no answer, so I ran some tests.

As the person who started the other thread thank you for that.

I must say I'm surprised it can do doubles so fast. Do you have data for division? With division algorithms, you'll need to test a wide range of denominators to get a good feel.

BTW, double is supposed to be 8-bytes: http://www.arduino.cc/en/Reference/Double
I don't know why sizeof() would return 4.

I must say I'm surprised it can do doubles so fast. Do you have data for division? With division algorithms, you'll need to test a wide range of denominators to get a good feel.

I just tried it with several different denominators & always get a result in the range of 34 microseconds. So floating point division is about 3-4 times slower than multiplication (9 usec).

Even so, that's about 30,000 divisions per second. This seems fast enough for (much) government work! :

BTW, double is supposed to be 8-bytes: http://www.arduino.cc/en/Reference/Double
I don't know why sizeof() would return 4.

I would tend to believe what the compiler is reporting, but I just posted something on this in Bugs/Suggestions, so we will see.

I just tried it with several different denominators & always get a result in the range of 34 microseconds. So floating point division is about 3-4 times slower than multiplication (9 usec).

Even so, that's about 30,000 divisions per second. This seems fast enough for (much) government work! :

Hey, I work for the government

34 microseconds is amazing. I originally tried to do my current project on a pic16F, and was getting results about a thousand times slower. It was at the point where I was trying to use 2 PICs, one basically as a floating-point processor, and the whole project got kludgy. Not to mention all the fun I had learning how to do floating point division in assembly on a processor that only does 8-bit integer add and subtract.

Because of the speed, it just works on the Ardunio. And the HLL is so much faster than messing around with assembly or JAL. What took me weeks on the PIC took an hour on the Arduino.

I would tend to believe what the compiler is reporting, but I just posted something on this in Bugs/Suggestions, so we will see.

I saw your post there, I'm looking forward to seeing the answer. If we trust the compiler, there's no difference beween float and double though. Float is good enough for my purposes though.

Yep, on the AVR's, float and double are both 32 bits.

Yep, on the AVR's, float and double are both 32 bits.

Thanks for checking it out & changing the docs.

Yep, on the AVR's, float and double are both 32 bits.

So are floats and doubles identical?

Confirmed, in winavr float == double (source: "WinAVR / gccavr does not support doubles, in other words: double is considered as float" in http://www.avrfreaks.net/wiki/index.php/Documentation:AVR_Float) and also from the maintainer of avr-libc: http://www.avrfreaks.net/index.php?module=PNphpBB2&file=viewtopic&t=50770&highlight=

and for most Arduino apps floats are more than sufficient

and for most Arduino apps floats are more than sufficient

So far, I haven't hit a situation where I absolutely had to have a double in an embedded project, but why does that statement make me think "640k ought to be enough for anyone"?

and for most Arduino apps floats are more than sufficient

So far, I haven't hit a situation where I absolutely had to have a double in an embedded project, but why does that statement make me think "640k ought to be enough for anyone"?

And you say that in the forum about the highly capable Arduino that has just 1k

So far, I haven't hit a situation where I absolutely had to have a double in an embedded project, but why does that statement make me think "640k ought to be enough for anyone"?

And you say that in the forum about the highly capable Arduino that has just 1k

If you need more memory than you can count in bytes, your runtime code is too sloppy. (please don't quote me on that ).

-- and to clarify, I mean code, not data.