Arduino Forum

Forum 2005-2010 (read only) => Software => Syntax & Programs => Topic started by: jmknapp on Apr 07, 2008, 07:57 pm

Title: Speed of floating point operations -- test results
Post by: jmknapp on Apr 07, 2008, 07:57 pm
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:

(http://i187.photobucket.com/albums/x269/jmknapp/speedtest.png)

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.
Title: Re: Speed of floating point operations -- test res
Post by: follower on Apr 08, 2008, 05:26 pm
Quote
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: http://www.cppreference.com/data_types.html)

--Phil.
Title: Re: Speed of floating point operations -- test res
Post by: jmknapp on Apr 08, 2008, 11:18 pm
Quote
I'm too lazy to check for myself, but won't a float be 4 bytes and a double be 8 bytes? (Referring to: http://www.cppreference.com/data_types.html)

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
Title: Re: Speed of floating point operations -- test res
Post by: Oracle on Apr 09, 2008, 04:02 am
Quote
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.
Title: Re: Speed of floating point operations -- test res
Post by: jmknapp on Apr 09, 2008, 12:42 pm
Quote

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!  ::)

Quote

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.
Title: Re: Speed of floating point operations -- test res
Post by: Oracle on Apr 09, 2008, 02:20 pm
Quote

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.

Quote

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.
Title: Re: Speed of floating point operations -- test res
Post by: mellis on Apr 09, 2008, 06:40 pm
Yep, on the AVR's, float and double are both 32 bits.
Title: Re: Speed of floating point operations -- test res
Post by: jmknapp on Apr 09, 2008, 08:08 pm
Quote
Yep, on the AVR's, float and double are both 32 bits.

Thanks for checking it out & changing the docs.
Title: Re: Speed of floating point operations -- test res
Post by: Oracle on Apr 09, 2008, 08:13 pm
Quote
Yep, on the AVR's, float and double are both 32 bits.

So are floats and doubles identical?
Title: Re: Speed of floating point operations -- test res
Post by: nkcelectronics on Apr 11, 2008, 07:32 am
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=
Title: Re: Speed of floating point operations -- test res
Post by: mem on Apr 11, 2008, 09:47 am
and for most Arduino apps floats are more than sufficient
Title: Re: Speed of floating point operations -- test res
Post by: Oracle on Apr 11, 2008, 04:35 pm
Quote
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"?
Title: Re: Speed of floating point operations -- test res
Post by: mem on Apr 11, 2008, 04:47 pm
Quote
Quote
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  ;)
Title: Re: Speed of floating point operations -- test res
Post by: Oracle on Apr 11, 2008, 05:37 pm
Quote
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"?
Quote
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.