7#include "../../third_party/json.hpp"
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))...);
27template <
typename Func,
typename... Args>
double exec_time(Func&& callback, Args&&... args) {
29 std::invoke(std::forward<Func>(callback), std::forward<Args>(args)...);
31 double total = (end - start) /
double(CLOCKS_PER_SEC) * 1000;
33 std::cout <<
"\033[33m" <<
"Total execution time: " << total <<
" ms\n";
35 std::cout <<
"\033[32m" <<
"Total execution time: " << total <<
" ms\n";
41template <
typename Func1,
typename Tuple1,
typename Func2,
typename Tuple2>
42int check_faster(Func1&& callback1,
const Tuple1& args1, Func2&& callback2,
const Tuple2& args2,
46 invoke_tuple(std::forward<Func1>(callback1), args1,
47 std::make_index_sequence<std::tuple_size_v<Tuple1>>{});
49 auto total1 = (end - start) /
double(CLOCKS_PER_SEC) * 1000;
53 invoke_tuple(std::forward<Func2>(callback2), args2,
54 std::make_index_sequence<std::tuple_size_v<Tuple2>>{});
56 auto total2 = (end - start) /
double(CLOCKS_PER_SEC) * 1000;
59 std::cout <<
"Function 1 execution time: " << total1 <<
" ms\n";
60 std::cout <<
"Function 2 execution time: " << total2 <<
" ms\n";
63 return (total1 > total2) ? 1 : 0;
66template <
typename Func,
typename Tuple>
67double complexity_analyzer_tuples(Func&& callback,
const Tuple& args) {
69 invoke_tuple(std::forward<Func>(callback), args,
70 std::make_index_sequence<std::tuple_size_v<Tuple>>{});
72 double total = (end - start) /
double(CLOCKS_PER_SEC) * 1000;
76template <
typename Tuple>
int sizeof_tuples(
const Tuple args) {
77 if (std::tuple_size<
decltype(args)>::value == 1) {
78 return std::get<0>(args);
81 return std::tuple_size<
decltype(args)>::value;
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';
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)...};
98 j[
"input_size"] = input_size;
99 j[
"execution_time"] = exec_time;
100 std::ofstream file(
"info.json");
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());