Last active
October 14, 2020 16:05
-
-
Save santiago-salas-v/348abc23f7e757453fc959a8072742ed to your computer and use it in GitHub Desktop.
compare time to calculate power function by 1) cmath pow, 2) log & exp and 3) direct multiplication.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> // for cin, cout | |
#include <cmath> // for log, exp, pow | |
#include <stdio.h> // for printf | |
#include "timeit.h" | |
using namespace std; // std::cout ,... | |
float pow1(float base, float exponent){ // method 1 through direct multiplication | |
float result=1; | |
for(int i=1;i<=exponent;i++)result*=base; | |
return result; | |
} | |
float pow2(float base, float exponent){ // method 2 through logarithmic multiplication | |
float result=1; | |
result=exp(exponent*log(base)); | |
return result; | |
} | |
float pow3(float base, float exponent){ // method 3 through builtin cmath algorighm | |
float result=1; | |
result=pow(base,exponent); | |
return result; | |
} | |
int main(){ | |
float x=5.4; | |
float y=20; | |
int loops=1000000; | |
float duration_us; // duration in microseconds | |
float duration_us_prev; | |
duration_us=timeit(x,y,pow1,loops); | |
cout << "duration: " << duration_us << " microseconds/loop" << "\n"; | |
duration_us_prev=duration_us; | |
duration_us=timeit(x,y,pow2,loops); | |
cout << "duration: " << duration_us << " microseconds/loop" << "\n"; | |
if(duration_us<duration_us_prev)cout << "log";else cout << "direct"; | |
printf(" multiplication is %g%% faster \n",fmax(duration_us,duration_us_prev)/fmin(duration_us,duration_us_prev)*100-100); | |
duration_us_prev=duration_us; | |
duration_us=timeit(x,y,pow3,loops); | |
cout << "duration: " << duration_us << " microseconds/loop" << "\n"; | |
if(duration_us<duration_us_prev)cout << "cmath";else cout << "log"; | |
printf(" multiplication is %g%% faster \n",fmax(duration_us,duration_us_prev)/fmin(duration_us,duration_us_prev)*100-100); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
float timeit(float base, float exponent, float pow(float,float), int loops){ | |
float result; | |
std::chrono::system_clock::time_point t1=std::chrono::high_resolution_clock::now(); | |
for(int i=1;i<=loops;i++)result=pow(base,exponent); | |
std::chrono::system_clock::time_point t2=std::chrono::high_resolution_clock::now(); | |
printf("Test %g^%g=%0.10g, performed %d loops, ",base,exponent,result,loops); | |
std::chrono::system_clock::duration time_diff=t2-t1; | |
float duration_us=std::chrono::duration_cast<std::chrono::microseconds>(time_diff).count(); | |
return duration_us/loops; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment