944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 896
  • C++ RSS
Nov 11th, 2009
0

float and double behavior

Expand Post »
I am trying to optimize code for Monte Carlo simulation. Even minute performance differences piles up after 100 million iterations and thus I need to squeeze every nanosecond from math operations!
One area where I thought I could save a lot stems from the fact that I only require precision of 4 significant digits. It therefore seems natural to use float rather than double.
However, some testing suggests that double still performs better! This is unexpected.
Why is it that despite the fact that float is 32 bits and double 64 bits, mathh functions are quicker to perform exp(double) and pow(double, double) than exp(float) and pow(float, float) (or even expf and powf)? Here is some code...
C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <math.h>
  3. #include <iostream>
  4. #include "Timer.h"
  5.  
  6. int main()
  7. {
  8. double a = 23.14;
  9. float c = 23.14;
  10. Timer t;
  11. t.tic();
  12. for (int i = 0; i < 10000000; i++)
  13. expf(c);
  14. cout<<"expf(float) returns " << expf(c)<<" and took "<<t.toc()<< " seconds." << endl;
  15. t.tic();
  16. for (int i = 0; i < 10000000; i++)
  17. exp(c);
  18. cout<<"exp(float) returns " << exp(c)<<" and took "<<t.toc()<< " seconds." << endl;
  19. t.tic();
  20. for (int i = 0; i < 10000000; i++)
  21. exp(a);
  22. cout<<"exp(double) returns " << exp(a)<<" and took "<<t.toc()<< " seconds." << endl;
  23. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
abie is offline Offline
1 posts
since Nov 2009
Nov 11th, 2009
-7
Re: float and double behavior
>>However, some testing suggests that double still performs better! This is unexpected.

Yup. floats are always converted to doubles when used as function parameters. Other factors may influence it too, such as the math coprocessor on your computer.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,959 posts
since Aug 2005
Nov 11th, 2009
1
Re: float and double behavior
Yup. floats are always converted to doubles when used as function parameters.
http://groups.google.com/group/comp....p.lang.c&pli=1
Quote ...
No, that's only true if
(1) you call the function without a prototype in scope, or
(2) it's a variable argument to a variadic function like 'printf'.
Last edited by Dave Sinkula; Nov 11th, 2009 at 12:59 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 11th, 2009
-7
Re: float and double behavior
Dave: Yes, I see you are correct. I wrote a short test program and had the compiler produce assembler instructions, which showed the same behavior as what you posted.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,959 posts
since Aug 2005
Nov 11th, 2009
0
Re: float and double behavior
floats are made for spaced optimization, it does not necessarily have to
be faster than double, especially in a 64bit CPU.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 11th, 2009
0
Re: float and double behavior
How about you show some code, so we can try to help you,
although monte carlo problem should be slow to execute as it is quite
easy to implement.
Last edited by firstPerson; Nov 11th, 2009 at 7:58 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: very basic c++ question on vectors
Next Thread in C++ Forum Timeline: waveOutWrite is creating complex wave instead of simple sine wave





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC