From 9a7640a898a1f7dd796ea81582fc61c678ea3f0f Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:43 +0100 Subject: [PATCH] feat(c++ karatsuba): measure the time taken for each method - --- languages/c++/code/karatsuba.cc | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/languages/c++/code/karatsuba.cc b/languages/c++/code/karatsuba.cc index 4bb8953..9ca95ee 100644 --- a/languages/c++/code/karatsuba.cc +++ b/languages/c++/code/karatsuba.cc @@ -1,6 +1,11 @@ #include #include +#include using namespace std; +using namespace std::chrono; + +typedef high_resolution_clock Clock; +typedef Clock::time_point ClockTime; double karatsuba(double num1, double num2) { int num1Length = to_string((int)num1).size(); @@ -37,9 +42,18 @@ int main() { double num2; cin >> num2; + ClockTime karatsubaBegin, karatsubaEnd; + karatsubaBegin = Clock::now(); double karatsubaResult = karatsuba(num1, num2); - double quadraticResult = num1 * num2; + karatsubaEnd = Clock::now(); + double karatsubaTime = duration_cast(karatsubaEnd - karatsubaBegin).count(); - cout << endl << "Quadratic Result: " + to_string(quadraticResult) << endl; - cout << "Karatsuba Result: " + to_string(karatsubaResult); + ClockTime quadraticBegin, quadraticEnd; + quadraticBegin = Clock::now(); + double quadraticResult = num1 * num2; + quadraticEnd = Clock::now(); + double quadraticTime = duration_cast(quadraticEnd - quadraticBegin).count(); + + cout << endl << "Quadratic Result: " << quadraticResult << " (" << quadraticTime << "ns)" << endl; + cout << "Karatsuba Result: " << karatsubaResult << " (" << karatsubaTime << "ns)"; }