Pages: 1 [2]   Go Down
Author Topic: Speed of floating point operations  (Read 1372 times)
0 Members and 1 Guest are viewing this topic.
Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged


Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12487
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 1
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley-wink

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.


 
« Last Edit: November 11, 2012, 07:56:32 am by zatalian » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Arduino Rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Pages: 1 [2]   Go Up
Jump to: