I'm writing a program that depends a lot on complex additions and multiplications. I wanted to know whether I should use gsl_complex
or std::complex
.
I don't seem to find a comparison online of how much better GSL complex arithmetic is as compared to std::complex
. A rudimentary google search didn't help me find a benchmarks page for GSL complex either.
I wrote a 20-line program that generates two random arrays of complex numbers (1e7 of them) and then checked how long addition and multiplication took using clock()
from <ctime>
. Using this method (without compiler optimisation) I got to know that gsl_complex_add
and gsl_complex_mul
are almost twice as fast as std::complex<double>
's +
and *
respectively. But I've never done this sort of thing before, so is this even the way you check which is faster?
Any links or suggestions would be helpful. Thanks!
EDIT:
Okay, so I tried again with a -O3 flag, and now the results are extremely different! std::complex<float>::operator+
is more than twice as fast as gsl_complex_add
, while gsl_complex_mul
is about 1.25 times as fast as std::complex<float>::operator*
. If I use double, gsl_complex_add
is about 30% faster than std::complex<double>::operator+
while std::complex<double>::operator*
is about 10% faster than gsl_complex_mul
. I only need float-level precision, but I've heard that double is faster (and memory is not an issue for me)! So now I'm really confused!