Sign in to follow this  
LonelyStar

Including cmath makes exp much slower

Recommended Posts

LonelyStar    192
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

Share this post


Link to post
Share on other sites
alvaro    21266
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.

Share this post


Link to post
Share on other sites
LonelyStar    192
Hi,

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

Share this post


Link to post
Share on other sites
cache_hit    614
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

Share this post


Link to post
Share on other sites
visitor    643
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?

Share this post


Link to post
Share on other sites
LonelyStar    192
Quote:
Original post by visitor
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?


If I do it with double, the difference vanishes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this