feat(c++ karatsuba): measure the time taken for each method

-
This commit is contained in:
newt 2024-10-09 18:02:43 +01:00
parent 146d1716ef
commit 9a7640a898

View file

@ -1,6 +1,11 @@
#include <iostream> #include <iostream>
#include <math.h> #include <math.h>
#include <chrono>
using namespace std; using namespace std;
using namespace std::chrono;
typedef high_resolution_clock Clock;
typedef Clock::time_point ClockTime;
double karatsuba(double num1, double num2) { double karatsuba(double num1, double num2) {
int num1Length = to_string((int)num1).size(); int num1Length = to_string((int)num1).size();
@ -37,9 +42,18 @@ int main() {
double num2; double num2;
cin >> num2; cin >> num2;
ClockTime karatsubaBegin, karatsubaEnd;
karatsubaBegin = Clock::now();
double karatsubaResult = karatsuba(num1, num2); double karatsubaResult = karatsuba(num1, num2);
double quadraticResult = num1 * num2; karatsubaEnd = Clock::now();
double karatsubaTime = duration_cast<nanoseconds>(karatsubaEnd - karatsubaBegin).count();
cout << endl << "Quadratic Result: " + to_string(quadraticResult) << endl; ClockTime quadraticBegin, quadraticEnd;
cout << "Karatsuba Result: " + to_string(karatsubaResult); quadraticBegin = Clock::now();
double quadraticResult = num1 * num2;
quadraticEnd = Clock::now();
double quadraticTime = duration_cast<nanoseconds>(quadraticEnd - quadraticBegin).count();
cout << endl << "Quadratic Result: " << quadraticResult << " (" << quadraticTime << "ns)" << endl;
cout << "Karatsuba Result: " << karatsubaResult << " (" << karatsubaTime << "ns)";
} }