complexのnormは遅い?
C++のcomplex
検証してみた。
ソース
#include <iostream> #include <complex> using namespace std; typedef complex<double> P; int main(int argc,char**argv){ int N = 10000; for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ P p(i,j); if(argv[1][0] == 'n'){ double no1 = norm(p); }else{ double no2 = p.real()*p.real() + p.imag()*p.imag(); } } } }
"-O2" したらそもそも消されてしまったので、最適化なしで。
./a.out x 2.68s user 0.00s system 99% cpu 2.709 total
norm使うと……
./a.out n 11.96s user 0.00s system 99% cpu 11.989 total
うーん。中の処理としては、abs(p)*abs(p)なのかな。