AlgoPlus v0.1.0
Loading...
Searching...
No Matches
timer.h
1#ifndef TIMER_H
2#define TIMER_H
3
4#include <filesystem>
5#include <utility>
6#ifdef __cplusplus
7#include "../../third_party/json.hpp"
8#include <array>
9#include <cstdlib>
10#include <ctime>
11#include <fstream>
12#include <functional>
13#include <iostream>
14#include <tuple>
15#endif
16
17using nlohmann::json;
18
19namespace TIMER {
20// Helper function to invoke a function with arguments from a tuple
21template <typename Func, typename Tuple, std::size_t... I>
22void invoke_tuple(Func&& func, Tuple&& tup, std::index_sequence<I...>) {
23 std::invoke(std::forward<Func>(func), std::get<I>(std::forward<Tuple>(tup))...);
24}
25
26// Function to measure execution time
27template <typename Func, typename... Args> double exec_time(Func&& callback, Args&&... args) {
28 auto start = clock();
29 std::invoke(std::forward<Func>(callback), std::forward<Args>(args)...);
30 auto end = clock();
31 double total = (end - start) / double(CLOCKS_PER_SEC) * 1000;
32 if (total > 1.0) {
33 std::cout << "\033[33m" << "Total execution time: " << total << " ms\n";
34 } else {
35 std::cout << "\033[32m" << "Total execution time: " << total << " ms\n";
36 }
37 return total;
38}
39
40// Function to compare execution times of two functions
41template <typename Func1, typename Tuple1, typename Func2, typename Tuple2>
42int check_faster(Func1&& callback1, const Tuple1& args1, Func2&& callback2, const Tuple2& args2,
43 int verbose = 0) {
44 // Run first function
45 auto start = clock();
46 invoke_tuple(std::forward<Func1>(callback1), args1,
47 std::make_index_sequence<std::tuple_size_v<Tuple1>>{});
48 auto end = clock();
49 auto total1 = (end - start) / double(CLOCKS_PER_SEC) * 1000;
50
51 // Run second function
52 start = clock();
53 invoke_tuple(std::forward<Func2>(callback2), args2,
54 std::make_index_sequence<std::tuple_size_v<Tuple2>>{});
55 end = clock();
56 auto total2 = (end - start) / double(CLOCKS_PER_SEC) * 1000;
57
58 if (verbose == 1) {
59 std::cout << "Function 1 execution time: " << total1 << " ms\n";
60 std::cout << "Function 2 execution time: " << total2 << " ms\n";
61 }
62
63 return (total1 > total2) ? 1 : 0;
64}
65
66template <typename Func, typename Tuple>
67double complexity_analyzer_tuples(Func&& callback, const Tuple& args) {
68 auto start = clock();
69 invoke_tuple(std::forward<Func>(callback), args,
70 std::make_index_sequence<std::tuple_size_v<Tuple>>{});
71 auto end = clock();
72 double total = (end - start) / double(CLOCKS_PER_SEC) * 1000;
73 return total;
74}
75
76template <typename Tuple> int sizeof_tuples(const Tuple args) {
77 if (std::tuple_size<decltype(args)>::value == 1) {
78 return std::get<0>(args);
79 }
80
81 return std::tuple_size<decltype(args)>::value;
82}
83
84template <typename Func, typename... Tuples>
85void time_complexity(Func&& callback, const Tuples&... tuples) {
86 if constexpr (sizeof...(tuples) < 5) {
87 std::cout << "\033[34m" << "Data with size: " << (sizeof...(tuples))
88 << " are not enough to generate time complexity analysis" << '\n';
89 return;
90 }
91
92 std::array<int, sizeof...(tuples)> input_size = {sizeof_tuples(tuples)...};
93 std::array<double, sizeof...(tuples)> exec_time = {
94 complexity_analyzer_tuples(callback, tuples)...};
95
96 // write to file
97 json j;
98 j["input_size"] = input_size;
99 j["execution_time"] = exec_time;
100 std::ofstream file("info.json");
101 file << j.dump(4);
102 file.close();
103
104 std::filesystem::path header_path = __FILE__;
105 std::filesystem::path script_path = header_path.parent_path() / "analyzer.py";
106 std::string command = "python3 " + script_path.string();
107 int rr = system(command.c_str());
108}
109} // namespace TIMER
110
111#endif