Including cmath makes exp much slower
Hi,
I have a strange problem:
I have a program using exp a lot.
If I do:
#include <math.h>
and use exp, the runtime of the program is 1 second.
If I do:
#include <cmath>
and use std::exp, the runtime of the program is 4 seconds.
If I do:
#include <cmath>
#include <math.h>
and use exp, the runtime of the program is 4 seconds.
The problem is, that I use libraries, which include cmath (even in another source file) and thereby my program gets slower.
I tried to make a simple test case, but the problem did not appear there.
So I do not know which part of my pretty large program triggers this.
Anyone ideas and/or experience with this?
Thanks!
nathan
Can you please post a complete sample program that shows the problem? I can probably figure out what's going on if I can play with it a bit.
EDIT: Nevermind. I missed the part where you said you didn't manage to reproduce the problem in a small program. My best guess is that you are converting between floats and doubles, or something like that. Perhaps try to keep the same exact types in the sample program.
EDIT: Nevermind. I missed the part where you said you didn't manage to reproduce the problem in a small program. My best guess is that you are converting between floats and doubles, or something like that. Perhaps try to keep the same exact types in the sample program.
Hi,
What A nice idea, this seems to be it. Here is a test program:
I probably can fix this, by removing all "using namespace std" (working on it ...).
But why can std::exp not handle floats proabably?
Thanks!
Nathan
What A nice idea, this seems to be it. Here is a test program:
1 #include <math.h> 2 #include <cmath> //Insert or remove this line to see the difference 3 4 using namespace std; 5 6 int main() 7 { 8 float index=0.0; 9 for(int i=0;i<1E8;++i) 10 { 11 index=exp(-index); 12 } 13 return 0; 14 }
I probably can fix this, by removing all "using namespace std" (working on it ...).
But why can std::exp not handle floats proabably?
Thanks!
Nathan
Not sure how gcc implements <cmath>, but on Visual Studio, it's defined like this:
namespace std
{
using ::exp;
}
So I'm not really sure why you'd see a difference if the only thing you were doing differently was using std::exp vs ::exp, since they are actually the same function.
Maybe the inclusion of cmath is bringing in a bunch of other header files which have static variables that require static initialization that takes some time. 3 seconds of static initialization time still seems like a lot though
namespace std
{
using ::exp;
}
So I'm not really sure why you'd see a difference if the only thing you were doing differently was using std::exp vs ::exp, since they are actually the same function.
Maybe the inclusion of cmath is bringing in a bunch of other header files which have static variables that require static initialization that takes some time. 3 seconds of static initialization time still seems like a lot though
Can't see any performance difference.
However the <math.h> version only works with doubles. The float version is only overloaded in <cmath>. What if you tried with doubles?
However the <math.h> version only works with doubles. The float version is only overloaded in <cmath>. What if you tried with doubles?
Ahh I guess that's the difference. Visual Studio MSVCRT has overloaded versions for float, double, and long double in math.h.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement