Need performance test on two codes

Heya,

Due to me simply not knowing how to do speed/RAM testing, I'd either like to know how to or have someone to do it for me for these two codes:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cmath>
#define PI 3.14159265

using namespace std;

int main()
{
   for(double x=0.0; x<2.0; x+=0.05)
      cout << "(" << cos(x*PI) << "," << sin(x*PI) << ")\n";
}


1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <complex>

using namespace std;

int main()
{
   for(double x=0.0; x<2.0; x+=0.05)
      cout << pow(complex<double>(0.0,1.0),2*x) << "\n";
}


The reason for this is because I recently discovered that i2x=cos(x*pi)+i*sin(x*pi), and I want to know if the current means of calculating sin and cos are faster than powers of i.
Well from my non professional standpoint you have two options. Count the ticks it takes for each to run, or:

Run each 1000, 10,000 , or 100,000 times and take not[e] of the difference in seconds.

I recommend the latter. You can use time(0) to get the number of seconds.

If you're on a windows system you can use GetTickCount();
linux system
gettimeofday();

Last edited on
If you're on any UNIX system you could do
$ time /path/to/binary
for example:
$ time find / -name "*"
would tell you how long it takes to list every file in your filesystem (probably quite a while).
Edit: Took mine 2m 47s.
Last edited on
Tested it, the "regular" cos and sin calculating works a lot faster than using powers of i.. D:
Dunno how the pow, sin and cos are calculated.
But I think that you could get a good approximation with low order Taylor's series for sin and cos.

ejx = cos(x) + j sin(x)
However the sin and cos are know to be periodic, that could be used to work with "nicer" numbers
Well, Taylor sucks for distant values (near pi). One way could be using sin(x) = cos(x-pi/2)
Another, using knows values interpolate a polynomial. Like (0,0); (pi/6,0.5); (pi/4, sqrt(2)/2); (pi/2, 1)
(there is the Gauss quadrature that ask for specific evaluation points)

It seems that the sin calculation is performed using a look-up table and interpolation.
The pow could use xy = z -> y ln(x) = ln(z)
So it will be binary search against logarithm and exponentiation.
Last edited on
visual studio 2010 has its own profiler just in case that's what you're using, I've been using it for months and didn't find this out until recently.
Don't use cout inside the loop, or you will be benchmarking cout, not the formula. IO is much much slower than evaluating expressions.
Last edited on
(sin and cos are implemented using lookup tables -- they should be quite fast)
Topic archived. No new replies allowed.