Go Down

Topic: Speed of floating point operations (Read 2108 times) previous topic - next topic

DuaneB

Which all goes to show what a nonsense measuring the performance and optimization of contrived sequences of code is.

If you have a real application - then it gets interesting.

Duane B.

Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

dhenry

Quote
All volatile says is it may not alter the stores or loads to the variable.


It tells the compiler not to assume that the value in a variable, even if it does not appear that the variable has not been written to.

The issue in this code is due to the resulting variable is not used so the division was optimized away. You can force a use of that division by assigning it to a (volatile) variable.

PeterH


Is there a way to remove the -Os compile option and get rid of all the optimizations? Just for the sake of this kind of exercises?


You are proposing to disable all optimisations in order to make it possible to measure the performance? Doesn't that render the performance measurements meaningless?
I only provide help via the forum - please do not contact me for private consultancy.

robtillaart

Quote
Which all goes to show what a nonsense measuring the performance and optimization of contrived sequences of code is.

If you have a real application - then it gets interesting.


Agree with you unless your goal is to learn about optimizations and how they are done. (there is always that other option ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

zatalian

#19
Nov 11, 2012, 01:53 pm Last Edit: Nov 11, 2012, 01:56 pm by zatalian Reason: 1

Quote
20-40 clockpulses


I am very surprised that its that fast, just to be sure, do you mean milliseconds or clock pulses ?

Duane B


clockpulses... This is the code i used :

Code: [Select]
void setup()
{
 Serial.begin(9600);
 TCCR1B &= 0xF8;
 TCCR1B |= (1 << CS10);
}

void loop()
{
 float fnumber1, fnumber2;
 volatile float fresult = 0.0;
 
 uint16_t time;
 unsigned long total;
 
 for (int i = 0; i < 10000; i++)
 {
   fnumber1 = random() / 1000.0;
   fnumber2 = random() / 1000.0;
 
   TCNT1 = 0;
   fresult = fnumber1 / fnumber2;
   time = TCNT1;
 
   total += time;
 }
 total /= 10000;
 
 Serial.print("delta: ");
 Serial.println(time, DEC);
 
 
 Serial.print(fnumber1, DEC);
 Serial.print(" / ");
 Serial.print(fnumber2, DEC);
 Serial.print(" = ");
 Serial.println(fresult, DEC);  
 

}




Quote
Which all goes to show what a nonsense measuring the performance and optimization of contrived sequences of code is.

If you have a real application - then it gets interesting.


Agree with you unless your goal is to learn about optimizations and how they are done. (there is always that other option ;)


Well, disabling optimizations can be useful to compare floating point operations versus integer operations. The whole point of this exercise was to know - before i have a complete project - if the arduino will be fast enough and if i will be able to use floating points or if I will have to do all the calculations with integers.

But in the end... these measurements will indeed be estimates and real measurements can only be taken in real programs. I totally agree with that statement.
Thanks to everybody for this very informative discussion.



chung

This is a good reference on the number of clockcycles required by an AVR chip to perform some mathematical operation (floating point operation). However, make sure not to invoke the function using some Arduino wrapper. As a rule of thumb, the lower your level of coding becomes, the faster the performance of your implementation will be, and the more difficult your code will become; so there's a balance you need to strike between efficiency and simplicity.
An Arduino development board costs €20~60. A pack of 20 Zener diodes to protect your board from almost certain damage costs less than €1...

Go Up