[bash]#include#include #include class Cplx { private: public: // data. double a[2]; Cplx(double r, double i) { a[0] = r; a[1] = i; }; #if 1 // fast Cplx powfast(const double n) const { const double r = ::pow(a[0]*a[0] + a[1]*a[1],n/2.0); // r^n const double theta = ::atan2(a[1],a[0]); // theta*n Cplx lval(r*::cos(theta*n),r*::sin(theta*n)); return lval; } #endif #if 1 // slow Cplx powslow(const double n) const { const double r = ::pow(a[0]*a[0] + a[1]*a[1],n/2.0); // r^n const double theta = ::atan2(a[1],a[0])*n; // theta*n Cplx lval(r*::cos(theta),r*::sin(theta)); return lval; } #endif const Cplx& operator+=(const Cplx &rval) { a[0] += rval.a[0]; a[1] += rval.a[1]; return *this; } } ; double wtime() { struct timeval thetime; gettimeofday(&thetime,NULL); return (double)thetime.tv_sec + (double)thetime.tv_usec/1.0e+6; } int main(int argc, char * argv[]) { int i; int niter = 30000000; Cplx a(0.5,0.02); Cplx b(0.6,-0.01); Cplx c(0.0,0.0); // test the fast version double ck1 = wtime(); for (i = 0; i < niter; i++) { c += b.powfast(1.4); } double ck2 = wtime(); printf("Result: %.15E %.15E\n",c.a[0],c.a[1]); c.a[0] = c.a[1] = 0.0; // test the slow version double ck3 = wtime(); for (i = 0; i < niter; i++) { c += b.powslow(1.4); } double ck4 = wtime(); printf("Result: %.15E %.15E\n",c.a[0],c.a[1]); printf("FAST: %.6f seconds.\n",ck2-ck1); printf("SLOW: %.6f seconds.\n",ck4-ck3); return(0); } [/bash]
Link Copied
[bash]Jim
[/bash]
[bash]#include#include #include class Cplx { private: public: // data. double a[2]; Cplx(double r, double i) { a[0] = r; a[1] = i; }; #if 1 Cplx pownopre(const double n) const { const double r = ::pow(a[0]*a[0] + a[1]*a[1],n/2.0); // r^n const double theta = ::atan2(a[1],a[0]); // theta*n Cplx lval(r*::cos(theta*n),r*::sin(theta*n)); return lval; } #endif #if 1 Cplx powpre(const double n) const { const double r = ::pow(a[0]*a[0] + a[1]*a[1],n/2.0); // r^n const double theta = ::atan2(a[1],a[0])*n; // theta*n Cplx lval(r*::cos(theta),r*::sin(theta)); return lval; } #endif const Cplx& operator+=(const Cplx &rval) { a[0] += rval.a[0]; a[1] += rval.a[1]; return *this; } } ; double wtime() { struct timeval thetime; gettimeofday(&thetime,NULL); return (double)thetime.tv_sec + (double)thetime.tv_usec/1.0e+6; } int main(int argc, char * argv[]) { int i; int niter = 30000000; Cplx a(0.5,0.02); Cplx b(0.6,-0.01); Cplx c(0.0,0.0); double arg = 1.4; double delta = 1.0/niter; // test the fast version double ck1 = wtime(); for (i = 0; i < niter; i++) { c += b.powpre(arg); arg += delta; } double ck2 = wtime(); printf("Result: %.15E %.15En",c.a[0],c.a[1]); c.a[0] = c.a[1] = 0.0; arg = 1.4; // test the slow version double ck3 = wtime(); for (i = 0; i < niter; i++) { c += b.pownopre(arg); arg += delta; } double ck4 = wtime(); printf("Result: %.15E %.15En",c.a[0],c.a[1]); printf("POW-PRE: %.6f seconds.n",ck2-ck1); printf("POW-NOPRE: %.6f seconds.n",ck4-ck3); return(0); } [/bash]
For more complete information about compiler optimizations, see our Optimization Notice.