실험 환경 : VS 2017, ISO C++17 표준(/std:c++17)
실험코드
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <time.h>
using namespace std;
class ClockTime
{
private:
clock_t start, end;
double time;
public:
ClockTime() { Init(); }
void Start()
{
start = clock();
}
double End(bool bPrintTime = false)
{
end = clock();
time = (double)(end - start) / CLOCKS_PER_SEC;
if (bPrintTime)
cout << time << "\n";
return time;
}
void Init()
{
start = end = time = 0;
}
};
int main()
{
const int runTime = 100;
const int eachRunTime = 1000000000; // 100,000,000 = 1억
std::ofstream ofilePow("powTime.txt"), ofileSqrt("sqrtTime.txt");
for (int epoch = 0; epoch < runTime; epoch++)
{
ClockTime myClock;
myClock.Start();
for (int i = 0; i < eachRunTime; i++)
pow(double(100), 0.5);
//cout << "pow: ";
string powTime = to_string( myClock.End());
ofilePow << powTime << "\n";;
//////////////////////////////////////////////////////
myClock.Init();
myClock.Start();
for (int i = 0; i < eachRunTime; i++)
sqrt(double(100));
//cout << "sqrt: ";
string sqrtTime = to_string(myClock.End());
ofileSqrt << sqrtTime << "\n";
}
ofilePow.close();
ofileSqrt.close();
return 0;
}
-> 각 코드를 1억번씩, 100번 수행
실험 결과
참고 답변:
https://stackoverflow.com/questions/17417490/difference-between-sqrtx-and-powx-0-5
댓글 영역