From ee97b3f100a41037019ad5f3d906ba481d27a233 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Wed, 16 Apr 2025 23:58:28 +0530 Subject: [PATCH 01/10] added timeit xmagic for native and lite --- CMakeLists.txt | 1 + src/xinterpreter.cpp | 2 + src/xmagics/execution.cpp | 223 ++++++++++++++++++++++++++++++++++++++ src/xmagics/execution.hpp | 51 +++++++++ 4 files changed, 277 insertions(+) create mode 100644 src/xmagics/execution.cpp create mode 100644 src/xmagics/execution.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index da8d5d76..3db0d7b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -214,6 +214,7 @@ set(XEUS_CPP_SRC src/xparser.cpp src/xutils.cpp src/xmagics/os.cpp + src/xmagics/execution.cpp ) if(NOT EMSCRIPTEN) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 85aa07b1..d97e8945 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -18,6 +18,7 @@ #include "xinput.hpp" #include "xinspect.hpp" #include "xmagics/os.hpp" +#include "xmagics/execution.hpp" #ifndef EMSCRIPTEN #include "xmagics/xassist.hpp" #endif @@ -373,6 +374,7 @@ __get_cxx_version () // timeit(&m_interpreter)); // preamble_manager["magics"].get_cast().register_magic("python", pythonexec()); preamble_manager["magics"].get_cast().register_magic("file", writefile()); + preamble_manager["magics"].get_cast().register_magic("timeit", timeit(Cpp::GetInterpreter())); #ifndef EMSCRIPTEN preamble_manager["magics"].get_cast().register_magic("xassist", xassist()); #endif diff --git a/src/xmagics/execution.cpp b/src/xmagics/execution.cpp new file mode 100644 index 00000000..f999cc77 --- /dev/null +++ b/src/xmagics/execution.cpp @@ -0,0 +1,223 @@ +/************************************************************************************ + * Copyright (c) 2023, xeus-cpp contributors * + * * + * Distributed under the terms of the BSD 3-Clause License. * + * * + * The full license is in the file LICENSE, distributed with this software. * + ************************************************************************************/ + + #include "execution.hpp" + #include "xeus-cpp/xinterpreter.hpp" + + #include "../xparser.hpp" + #include "clang/Interpreter/CppInterOp.h" + + namespace xcpp + { + struct StreamRedirectRAII { + std::string &err; + StreamRedirectRAII(std::string &e) : err(e) { + Cpp::BeginStdStreamCapture(Cpp::kStdErr); + Cpp::BeginStdStreamCapture(Cpp::kStdOut); + } + ~StreamRedirectRAII() { + std::string out = Cpp::EndStdStreamCapture(); + err = Cpp::EndStdStreamCapture(); + } + }; + + timeit::timeit(Cpp::TInterp_t p) : m_interpreter(p) + { + bool compilation_result = true; + std::string err; + compilation_result = Cpp::Process("#include \n#include \n"); + + // Define the reusable timing function once + std::string timing_function = R"( + double get_elapsed_time(std::size_t num_iterations, void (*func)()) { + auto _t2 = std::chrono::high_resolution_clock::now(); + for (std::size_t _i = 0; _i < num_iterations; ++_i) { + func(); + } + auto _t3 = std::chrono::high_resolution_clock::now(); + return std::chrono::duration_cast(_t3 - _t2).count(); + } + )"; + compilation_result = Cpp::Process(timing_function.c_str()); + } + + void timeit::get_options(argparser& argpars) + { + argpars.add_description("Time execution of a C++ statement or expression"); + argpars.add_argument("-n", "--number") + .help("execute the given statement n times in a loop. If this value is not given, a fitting value is chosen") + .default_value(0) + .scan<'i', int>(); + argpars.add_argument("-r", "--repeat") + .help("repeat the loop iteration r times and take the best result") + .default_value(7) + .scan<'i', int>(); + argpars.add_argument("-p", "--precision") + .help("use a precision of p digits to display the timing result") + .default_value(3) + .scan<'i', int>(); + argpars.add_argument("expression").help("expression to be evaluated").remaining(); + argpars.add_argument("-h", "--help") + .action([&](const std::string& /*unused*/) { std::cout << argpars.help().str(); }) + .default_value(false) + .help("shows help message") + .implicit_value(true) + .nargs(0); + } + + std::string timeit::inner(std::size_t number, const std::string& code) const + { + static std::size_t counter = 0; // Ensure unique lambda names + std::string unique_id = std::to_string(counter++); + std::string timeit_code = ""; + timeit_code += "auto user_code_" + unique_id + " = []() {\n"; + timeit_code += " " + code + "\n"; + timeit_code += "};\n"; + timeit_code += "get_elapsed_time(" + std::to_string(number) + ", user_code_" + unique_id + ")\n"; + return timeit_code; + } + + std::string timeit::_format_time(double timespan, std::size_t precision) const + { + std::vector units{"s", "ms", "us", "ns"}; + std::vector scaling{1, 1e3, 1e6, 1e9}; + std::ostringstream output; + int order; + + if (timespan > 0.0) + { + order = std::min(-static_cast(std::floor(std::floor(std::log10(timespan)) / 3)), 3); + } + else + { + order = 3; + } + output.precision(precision); + output << timespan * scaling[order] << " " << units[order]; + return output.str(); + } + + void timeit::execute(std::string& line, std::string& cell) + { + argparser argpars("timeit", XEUS_CPP_VERSION, argparse::default_arguments::none); + get_options(argpars); + argpars.parse(line); + + int number = argpars.get("-n"); + int repeat = argpars.get("-r"); + int precision = argpars.get("-p"); + + std::string code; + try + { + const auto& v = argpars.get>("expression"); + for (const auto& s : v) + { + code += " " + s; + } + } + catch (std::logic_error& e) + { + if (trim(cell).empty() && (argpars["-h"] == false)) + { + std::cerr << "No expression given to evaluate" << std::endl; + } + } + + code += cell; + if (trim(code).empty()) + { + return; + } + + auto errorlevel = 0; + std::string ename; + std::string evalue; + std::string output; + std::string err; + bool hadError = false; + + try + { + if (number == 0) + { + for (std::size_t n = 0; n < 10; ++n) + { + number = std::pow(10, n); + std::string timeit_code = inner(number, code); + std::ostringstream buffer_out, buffer_err; + std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); + std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); + StreamRedirectRAII R(err); + auto res_ptr = Cpp::Evaluate(timeit_code.c_str(), &hadError); + std::cout.rdbuf(old_cout); + std::cerr.rdbuf(old_cerr); + output = std::to_string(res_ptr); + err += buffer_err.str(); + double elapsed_time = std::stod(output) * 1e-6; + if (elapsed_time >= 0.2) + { + break; + } + } + } + + std::vector all_runs; + double mean = 0; + double stdev = 0; + for (std::size_t r = 0; r < static_cast(repeat); ++r) + { + std::string timeit_code = inner(number, code); + std::ostringstream buffer_out, buffer_err; + std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); + std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); + StreamRedirectRAII R(err); + auto res_ptr = Cpp::Evaluate(timeit_code.c_str(), &hadError); + std::cout.rdbuf(old_cout); + std::cerr.rdbuf(old_cerr); + output = std::to_string(res_ptr); + err += buffer_err.str(); + double elapsed_time = std::stod(output) * 1e-6; + all_runs.push_back(elapsed_time / number); + mean += all_runs.back(); + } + mean /= repeat; + for (std::size_t r = 0; r < static_cast(repeat); ++r) + { + stdev += (all_runs[r] - mean) * (all_runs[r] - mean); + } + stdev = std::sqrt(stdev / repeat); + + std::cout << _format_time(mean, precision) << " +- " << _format_time(stdev, precision); + std::cout << " per loop (mean +- std. dev. of " << repeat << " run" + << ((repeat == 1) ? ", " : "s "); + std::cout << number << " loop" << ((number == 1) ? "" : "s") << " each)" << std::endl; + } + catch (std::exception& e) + { + errorlevel = 1; + ename = "Standard Exception: "; + evalue = e.what(); + } + catch (...) + { + errorlevel = 1; + ename = "Error: "; + } + + if (hadError) + { + errorlevel = 1; + ename = "Error: "; + evalue = "Compilation error! " + err; + std::cerr << err; + std::cerr << "Error: " << evalue << std::endl; + std::cerr << "Error: " << err << std::endl; + } + } + } \ No newline at end of file diff --git a/src/xmagics/execution.hpp b/src/xmagics/execution.hpp new file mode 100644 index 00000000..bba65ddc --- /dev/null +++ b/src/xmagics/execution.hpp @@ -0,0 +1,51 @@ +/************************************************************************************ + * Copyright (c) 2023, xeus-cpp contributors * + * * + * Distributed under the terms of the BSD 3-Clause License. * + * * + * The full license is in the file LICENSE, distributed with this software. * + ************************************************************************************/ + +#ifndef XMAGICS_EXECUTION_HPP +#define XMAGICS_EXECUTION_HPP + +#include +#include + +#include "clang/Interpreter/CppInterOp.h" + +#include "xeus-cpp/xmagics.hpp" +#include "xeus-cpp/xoptions.hpp" + +namespace xcpp +{ + class timeit : public xmagic_line_cell + { + public: + + timeit(void* p); + + virtual void operator()(const std::string& line) override + { + std::string cline = line; + std::string cell = ""; + execute(cline, cell); + } + + virtual void operator()(const std::string& line, const std::string& cell) override + { + std::string cline = line; + std::string ccell = cell; + execute(cline, ccell); + } + + private: + Cpp::TInterp_t m_interpreter; + void get_options(argparser &argpars); + std::string inner(std::size_t number, const std::string& code) const; + std::string _format_time(double timespan, std::size_t precision) const; + void execute(std::string& line, std::string& cell); + std::string wrap_code(const std::string& code) const; + }; +} +#endif \ No newline at end of file From a5c5393177ec156aa4123c7217ef37d9478b1597 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Wed, 16 Apr 2025 23:59:27 +0530 Subject: [PATCH 02/10] added timeit xmagic for native and lite --- src/xmagics/execution.cpp | 425 ++++++++++++++++++++------------------ 1 file changed, 219 insertions(+), 206 deletions(-) diff --git a/src/xmagics/execution.cpp b/src/xmagics/execution.cpp index f999cc77..068210d6 100644 --- a/src/xmagics/execution.cpp +++ b/src/xmagics/execution.cpp @@ -6,34 +6,41 @@ * The full license is in the file LICENSE, distributed with this software. * ************************************************************************************/ - #include "execution.hpp" - #include "xeus-cpp/xinterpreter.hpp" - - #include "../xparser.hpp" - #include "clang/Interpreter/CppInterOp.h" - - namespace xcpp - { - struct StreamRedirectRAII { - std::string &err; - StreamRedirectRAII(std::string &e) : err(e) { - Cpp::BeginStdStreamCapture(Cpp::kStdErr); - Cpp::BeginStdStreamCapture(Cpp::kStdOut); - } - ~StreamRedirectRAII() { - std::string out = Cpp::EndStdStreamCapture(); - err = Cpp::EndStdStreamCapture(); - } - }; - - timeit::timeit(Cpp::TInterp_t p) : m_interpreter(p) - { - bool compilation_result = true; - std::string err; - compilation_result = Cpp::Process("#include \n#include \n"); - - // Define the reusable timing function once - std::string timing_function = R"( +#include "execution.hpp" +#include "xeus-cpp/xinterpreter.hpp" + +#include "../xparser.hpp" +#include "clang/Interpreter/CppInterOp.h" + +namespace xcpp +{ + struct StreamRedirectRAII + { + std::string& err; + + StreamRedirectRAII(std::string& e) + : err(e) + { + Cpp::BeginStdStreamCapture(Cpp::kStdErr); + Cpp::BeginStdStreamCapture(Cpp::kStdOut); + } + + ~StreamRedirectRAII() + { + std::string out = Cpp::EndStdStreamCapture(); + err = Cpp::EndStdStreamCapture(); + } + }; + + timeit::timeit(Cpp::TInterp_t p) + : m_interpreter(p) + { + bool compilation_result = true; + std::string err; + compilation_result = Cpp::Process("#include \n#include \n"); + + // Define the reusable timing function once + std::string timing_function = R"( double get_elapsed_time(std::size_t num_iterations, void (*func)()) { auto _t2 = std::chrono::high_resolution_clock::now(); for (std::size_t _i = 0; _i < num_iterations; ++_i) { @@ -43,181 +50,187 @@ return std::chrono::duration_cast(_t3 - _t2).count(); } )"; - compilation_result = Cpp::Process(timing_function.c_str()); - } - - void timeit::get_options(argparser& argpars) - { - argpars.add_description("Time execution of a C++ statement or expression"); - argpars.add_argument("-n", "--number") - .help("execute the given statement n times in a loop. If this value is not given, a fitting value is chosen") - .default_value(0) - .scan<'i', int>(); - argpars.add_argument("-r", "--repeat") - .help("repeat the loop iteration r times and take the best result") - .default_value(7) - .scan<'i', int>(); - argpars.add_argument("-p", "--precision") - .help("use a precision of p digits to display the timing result") - .default_value(3) - .scan<'i', int>(); - argpars.add_argument("expression").help("expression to be evaluated").remaining(); - argpars.add_argument("-h", "--help") - .action([&](const std::string& /*unused*/) { std::cout << argpars.help().str(); }) - .default_value(false) - .help("shows help message") - .implicit_value(true) - .nargs(0); - } - - std::string timeit::inner(std::size_t number, const std::string& code) const - { - static std::size_t counter = 0; // Ensure unique lambda names - std::string unique_id = std::to_string(counter++); - std::string timeit_code = ""; - timeit_code += "auto user_code_" + unique_id + " = []() {\n"; - timeit_code += " " + code + "\n"; - timeit_code += "};\n"; - timeit_code += "get_elapsed_time(" + std::to_string(number) + ", user_code_" + unique_id + ")\n"; - return timeit_code; - } - - std::string timeit::_format_time(double timespan, std::size_t precision) const - { - std::vector units{"s", "ms", "us", "ns"}; - std::vector scaling{1, 1e3, 1e6, 1e9}; - std::ostringstream output; - int order; - - if (timespan > 0.0) - { - order = std::min(-static_cast(std::floor(std::floor(std::log10(timespan)) / 3)), 3); - } - else - { - order = 3; - } - output.precision(precision); - output << timespan * scaling[order] << " " << units[order]; - return output.str(); - } - - void timeit::execute(std::string& line, std::string& cell) - { - argparser argpars("timeit", XEUS_CPP_VERSION, argparse::default_arguments::none); - get_options(argpars); - argpars.parse(line); - - int number = argpars.get("-n"); - int repeat = argpars.get("-r"); - int precision = argpars.get("-p"); - - std::string code; - try - { - const auto& v = argpars.get>("expression"); - for (const auto& s : v) - { - code += " " + s; - } - } - catch (std::logic_error& e) - { - if (trim(cell).empty() && (argpars["-h"] == false)) - { - std::cerr << "No expression given to evaluate" << std::endl; - } - } - - code += cell; - if (trim(code).empty()) - { - return; - } - - auto errorlevel = 0; - std::string ename; - std::string evalue; - std::string output; - std::string err; - bool hadError = false; - - try - { - if (number == 0) - { - for (std::size_t n = 0; n < 10; ++n) - { - number = std::pow(10, n); - std::string timeit_code = inner(number, code); - std::ostringstream buffer_out, buffer_err; - std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); - std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); - StreamRedirectRAII R(err); - auto res_ptr = Cpp::Evaluate(timeit_code.c_str(), &hadError); - std::cout.rdbuf(old_cout); - std::cerr.rdbuf(old_cerr); - output = std::to_string(res_ptr); - err += buffer_err.str(); - double elapsed_time = std::stod(output) * 1e-6; - if (elapsed_time >= 0.2) - { - break; - } - } - } - - std::vector all_runs; - double mean = 0; - double stdev = 0; - for (std::size_t r = 0; r < static_cast(repeat); ++r) - { - std::string timeit_code = inner(number, code); - std::ostringstream buffer_out, buffer_err; - std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); - std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); - StreamRedirectRAII R(err); - auto res_ptr = Cpp::Evaluate(timeit_code.c_str(), &hadError); - std::cout.rdbuf(old_cout); - std::cerr.rdbuf(old_cerr); - output = std::to_string(res_ptr); - err += buffer_err.str(); - double elapsed_time = std::stod(output) * 1e-6; - all_runs.push_back(elapsed_time / number); - mean += all_runs.back(); - } - mean /= repeat; - for (std::size_t r = 0; r < static_cast(repeat); ++r) - { - stdev += (all_runs[r] - mean) * (all_runs[r] - mean); - } - stdev = std::sqrt(stdev / repeat); - - std::cout << _format_time(mean, precision) << " +- " << _format_time(stdev, precision); - std::cout << " per loop (mean +- std. dev. of " << repeat << " run" - << ((repeat == 1) ? ", " : "s "); - std::cout << number << " loop" << ((number == 1) ? "" : "s") << " each)" << std::endl; - } - catch (std::exception& e) - { - errorlevel = 1; - ename = "Standard Exception: "; - evalue = e.what(); - } - catch (...) - { - errorlevel = 1; - ename = "Error: "; - } - - if (hadError) - { - errorlevel = 1; - ename = "Error: "; - evalue = "Compilation error! " + err; - std::cerr << err; - std::cerr << "Error: " << evalue << std::endl; - std::cerr << "Error: " << err << std::endl; - } - } - } \ No newline at end of file + compilation_result = Cpp::Process(timing_function.c_str()); + } + + void timeit::get_options(argparser& argpars) + { + argpars.add_description("Time execution of a C++ statement or expression"); + argpars.add_argument("-n", "--number") + .help("execute the given statement n times in a loop. If this value is not given, a fitting value is chosen" + ) + .default_value(0) + .scan<'i', int>(); + argpars.add_argument("-r", "--repeat") + .help("repeat the loop iteration r times and take the best result") + .default_value(7) + .scan<'i', int>(); + argpars.add_argument("-p", "--precision") + .help("use a precision of p digits to display the timing result") + .default_value(3) + .scan<'i', int>(); + argpars.add_argument("expression").help("expression to be evaluated").remaining(); + argpars.add_argument("-h", "--help") + .action( + [&](const std::string& /*unused*/) + { + std::cout << argpars.help().str(); + } + ) + .default_value(false) + .help("shows help message") + .implicit_value(true) + .nargs(0); + } + + std::string timeit::inner(std::size_t number, const std::string& code) const + { + static std::size_t counter = 0; // Ensure unique lambda names + std::string unique_id = std::to_string(counter++); + std::string timeit_code = ""; + timeit_code += "auto user_code_" + unique_id + " = []() {\n"; + timeit_code += " " + code + "\n"; + timeit_code += "};\n"; + timeit_code += "get_elapsed_time(" + std::to_string(number) + ", user_code_" + unique_id + ")\n"; + return timeit_code; + } + + std::string timeit::_format_time(double timespan, std::size_t precision) const + { + std::vector units{"s", "ms", "us", "ns"}; + std::vector scaling{1, 1e3, 1e6, 1e9}; + std::ostringstream output; + int order; + + if (timespan > 0.0) + { + order = std::min(-static_cast(std::floor(std::floor(std::log10(timespan)) / 3)), 3); + } + else + { + order = 3; + } + output.precision(precision); + output << timespan * scaling[order] << " " << units[order]; + return output.str(); + } + + void timeit::execute(std::string& line, std::string& cell) + { + argparser argpars("timeit", XEUS_CPP_VERSION, argparse::default_arguments::none); + get_options(argpars); + argpars.parse(line); + + int number = argpars.get("-n"); + int repeat = argpars.get("-r"); + int precision = argpars.get("-p"); + + std::string code; + try + { + const auto& v = argpars.get>("expression"); + for (const auto& s : v) + { + code += " " + s; + } + } + catch (std::logic_error& e) + { + if (trim(cell).empty() && (argpars["-h"] == false)) + { + std::cerr << "No expression given to evaluate" << std::endl; + } + } + + code += cell; + if (trim(code).empty()) + { + return; + } + + auto errorlevel = 0; + std::string ename; + std::string evalue; + std::string output; + std::string err; + bool hadError = false; + + try + { + if (number == 0) + { + for (std::size_t n = 0; n < 10; ++n) + { + number = std::pow(10, n); + std::string timeit_code = inner(number, code); + std::ostringstream buffer_out, buffer_err; + std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); + std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); + StreamRedirectRAII R(err); + auto res_ptr = Cpp::Evaluate(timeit_code.c_str(), &hadError); + std::cout.rdbuf(old_cout); + std::cerr.rdbuf(old_cerr); + output = std::to_string(res_ptr); + err += buffer_err.str(); + double elapsed_time = std::stod(output) * 1e-6; + if (elapsed_time >= 0.2) + { + break; + } + } + } + + std::vector all_runs; + double mean = 0; + double stdev = 0; + for (std::size_t r = 0; r < static_cast(repeat); ++r) + { + std::string timeit_code = inner(number, code); + std::ostringstream buffer_out, buffer_err; + std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); + std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); + StreamRedirectRAII R(err); + auto res_ptr = Cpp::Evaluate(timeit_code.c_str(), &hadError); + std::cout.rdbuf(old_cout); + std::cerr.rdbuf(old_cerr); + output = std::to_string(res_ptr); + err += buffer_err.str(); + double elapsed_time = std::stod(output) * 1e-6; + all_runs.push_back(elapsed_time / number); + mean += all_runs.back(); + } + mean /= repeat; + for (std::size_t r = 0; r < static_cast(repeat); ++r) + { + stdev += (all_runs[r] - mean) * (all_runs[r] - mean); + } + stdev = std::sqrt(stdev / repeat); + + std::cout << _format_time(mean, precision) << " +- " << _format_time(stdev, precision); + std::cout << " per loop (mean +- std. dev. of " << repeat << " run" + << ((repeat == 1) ? ", " : "s "); + std::cout << number << " loop" << ((number == 1) ? "" : "s") << " each)" << std::endl; + } + catch (std::exception& e) + { + errorlevel = 1; + ename = "Standard Exception: "; + evalue = e.what(); + } + catch (...) + { + errorlevel = 1; + ename = "Error: "; + } + + if (hadError) + { + errorlevel = 1; + ename = "Error: "; + evalue = "Compilation error! " + err; + std::cerr << err; + std::cerr << "Error: " << evalue << std::endl; + std::cerr << "Error: " << err << std::endl; + } + } +} \ No newline at end of file From d2703cd8473b36696e6cbcbe1e041fd31e9b0c51 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Thu, 17 Apr 2025 00:11:48 +0530 Subject: [PATCH 03/10] clang format --- src/xinterpreter.cpp | 118 +++++++++++++++++++++++--------------- src/xmagics/execution.hpp | 3 +- 2 files changed, 75 insertions(+), 46 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index d97e8945..e7f017be 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -17,8 +17,8 @@ #include "xinput.hpp" #include "xinspect.hpp" -#include "xmagics/os.hpp" #include "xmagics/execution.hpp" +#include "xmagics/os.hpp" #ifndef EMSCRIPTEN #include "xmagics/xassist.hpp" #endif @@ -27,47 +27,66 @@ using Args = std::vector; -void* createInterpreter(const Args &ExtraArgs = {}) { - Args ClangArgs = {/*"-xc++"*/"-v"}; // ? {"-Xclang", "-emit-llvm-only", "-Xclang", "-diagnostic-log-file", "-Xclang", "-", "-xc++"}; +void* createInterpreter(const Args& ExtraArgs = {}) +{ + Args ClangArgs = {/*"-xc++"*/ "-v"}; // ? {"-Xclang", "-emit-llvm-only", "-Xclang", + // "-diagnostic-log-file", "-Xclang", "-", "-xc++"}; #ifdef EMSCRIPTEN - ClangArgs.push_back("-std=c++20"); + ClangArgs.push_back("-std=c++20"); #else - if (std::find_if(ExtraArgs.begin(), ExtraArgs.end(), [](const std::string& s) { - return s == "-resource-dir";}) == ExtraArgs.end()) { - std::string resource_dir = Cpp::DetectResourceDir(); - if (resource_dir.empty()) - std::cerr << "Failed to detect the resource-dir\n"; - ClangArgs.push_back("-resource-dir"); - ClangArgs.push_back(resource_dir.c_str()); - } - std::vector CxxSystemIncludes; - Cpp::DetectSystemCompilerIncludePaths(CxxSystemIncludes); - for (const std::string& CxxInclude : CxxSystemIncludes) { - ClangArgs.push_back("-isystem"); - ClangArgs.push_back(CxxInclude.c_str()); - } + if (std::find_if( + ExtraArgs.begin(), + ExtraArgs.end(), + [](const std::string& s) + { + return s == "-resource-dir"; + } + ) + == ExtraArgs.end()) + { + std::string resource_dir = Cpp::DetectResourceDir(); + if (resource_dir.empty()) + { + std::cerr << "Failed to detect the resource-dir\n"; + } + ClangArgs.push_back("-resource-dir"); + ClangArgs.push_back(resource_dir.c_str()); + } + std::vector CxxSystemIncludes; + Cpp::DetectSystemCompilerIncludePaths(CxxSystemIncludes); + for (const std::string& CxxInclude : CxxSystemIncludes) + { + ClangArgs.push_back("-isystem"); + ClangArgs.push_back(CxxInclude.c_str()); + } #endif - ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end()); - // FIXME: We should process the kernel input options and conditionally pass - // the gpu args here. - return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/); + ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end()); + // FIXME: We should process the kernel input options and conditionally pass + // the gpu args here. + return Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/); } using namespace std::placeholders; namespace xcpp { - struct StreamRedirectRAII { - std::string &err; - StreamRedirectRAII(std::string &e) : err(e) { - Cpp::BeginStdStreamCapture(Cpp::kStdErr); - Cpp::BeginStdStreamCapture(Cpp::kStdOut); - } - ~StreamRedirectRAII() { - std::string out = Cpp::EndStdStreamCapture(); - err = Cpp::EndStdStreamCapture(); - std::cout << out; - } + struct StreamRedirectRAII + { + std::string& err; + + StreamRedirectRAII(std::string& e) + : err(e) + { + Cpp::BeginStdStreamCapture(Cpp::kStdErr); + Cpp::BeginStdStreamCapture(Cpp::kStdOut); + } + + ~StreamRedirectRAII() + { + std::string out = Cpp::EndStdStreamCapture(); + err = Cpp::EndStdStreamCapture(); + std::cout << out; + } }; void interpreter::configure_impl() @@ -102,14 +121,14 @@ __get_cxx_version () return std::to_string(cxx_version); } - interpreter::interpreter(int argc, const char* const* argv) : - xmagics() + interpreter::interpreter(int argc, const char* const* argv) + : xmagics() , p_cout_strbuf(nullptr) , p_cerr_strbuf(nullptr) , m_cout_buffer(std::bind(&interpreter::publish_stdout, this, _1)) , m_cerr_buffer(std::bind(&interpreter::publish_stderr, this, _1)) { - //NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic) + // NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic) createInterpreter(Args(argv ? argv + 1 : argv, argv + argc)); m_version = get_stdopt(); redirect_output(); @@ -212,10 +231,11 @@ __get_cxx_version () // // JupyterLab displays the "{ename}: {evalue}" if the traceback is // empty. - if (evalue.size() < 4) { + if (evalue.size() < 4) + { ename = " "; } - std::vector traceback({ename + evalue}); + std::vector traceback({ename + evalue}); if (!config.silent) { publish_execution_error(ename, evalue, traceback); @@ -258,7 +278,8 @@ __get_cxx_version () Cpp::CodeComplete(results, code.c_str(), 1, _cursor_pos + 1); - return xeus::create_complete_reply(results /*matches*/, + return xeus::create_complete_reply( + results /*matches*/, cursor_pos - to_complete.length() /*cursor_start*/, cursor_pos /*cursor_end*/ ); @@ -279,13 +300,17 @@ __get_cxx_version () nl::json interpreter::is_complete_request_impl(const std::string& code) { - if (!code.empty() && code[code.size() - 1] == '\\') { + if (!code.empty() && code[code.size() - 1] == '\\') + { auto found = code.rfind('\n'); if (found == std::string::npos) + { found = -1; + } auto found1 = found++; - while (isspace(code[++found1])) ; - return xeus::create_is_complete_reply("incomplete", code.substr(found, found1-found)); + while (isspace(code[++found1])) + ; + return xeus::create_is_complete_reply("incomplete", code.substr(found, found1 - found)); } return xeus::create_is_complete_reply("complete"); @@ -359,11 +384,11 @@ __get_cxx_version () void interpreter::init_preamble() { - //NOLINTBEGIN(cppcoreguidelines-owning-memory) + // NOLINTBEGIN(cppcoreguidelines-owning-memory) preamble_manager.register_preamble("introspection", std::make_unique()); preamble_manager.register_preamble("magics", std::make_unique()); preamble_manager.register_preamble("shell", std::make_unique()); - //NOLINTEND(cppcoreguidelines-owning-memory) + // NOLINTEND(cppcoreguidelines-owning-memory) } void interpreter::init_magic() @@ -374,7 +399,10 @@ __get_cxx_version () // timeit(&m_interpreter)); // preamble_manager["magics"].get_cast().register_magic("python", pythonexec()); preamble_manager["magics"].get_cast().register_magic("file", writefile()); - preamble_manager["magics"].get_cast().register_magic("timeit", timeit(Cpp::GetInterpreter())); + preamble_manager["magics"].get_cast().register_magic( + "timeit", + timeit(Cpp::GetInterpreter()) + ); #ifndef EMSCRIPTEN preamble_manager["magics"].get_cast().register_magic("xassist", xassist()); #endif diff --git a/src/xmagics/execution.hpp b/src/xmagics/execution.hpp index bba65ddc..383411ae 100644 --- a/src/xmagics/execution.hpp +++ b/src/xmagics/execution.hpp @@ -40,8 +40,9 @@ namespace xcpp } private: + Cpp::TInterp_t m_interpreter; - void get_options(argparser &argpars); + void get_options(argparser& argpars); std::string inner(std::size_t number, const std::string& code) const; std::string _format_time(double timespan, std::size_t precision) const; void execute(std::string& line, std::string& cell); From c3a0565b3ebcc29128ae9f57817e3f6067e5f611 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Thu, 17 Apr 2025 14:36:45 +0530 Subject: [PATCH 04/10] added tests for timeit --- src/xinterpreter.cpp | 5 +--- src/xmagics/execution.cpp | 50 ++++++++++++++++++++++----------------- src/xmagics/execution.hpp | 9 ++++--- test/test_interpreter.cpp | 41 +++++++++++++++++++++++++++++--- 4 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index e7f017be..6cc2dab0 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -399,10 +399,7 @@ __get_cxx_version () // timeit(&m_interpreter)); // preamble_manager["magics"].get_cast().register_magic("python", pythonexec()); preamble_manager["magics"].get_cast().register_magic("file", writefile()); - preamble_manager["magics"].get_cast().register_magic( - "timeit", - timeit(Cpp::GetInterpreter()) - ); + preamble_manager["magics"].get_cast().register_magic("timeit", timeit()); #ifndef EMSCRIPTEN preamble_manager["magics"].get_cast().register_magic("xassist", xassist()); #endif diff --git a/src/xmagics/execution.cpp b/src/xmagics/execution.cpp index 068210d6..50255647 100644 --- a/src/xmagics/execution.cpp +++ b/src/xmagics/execution.cpp @@ -32,25 +32,10 @@ namespace xcpp } }; - timeit::timeit(Cpp::TInterp_t p) - : m_interpreter(p) - { - bool compilation_result = true; - std::string err; - compilation_result = Cpp::Process("#include \n#include \n"); + int timeit::exec_counter = 0; - // Define the reusable timing function once - std::string timing_function = R"( - double get_elapsed_time(std::size_t num_iterations, void (*func)()) { - auto _t2 = std::chrono::high_resolution_clock::now(); - for (std::size_t _i = 0; _i < num_iterations; ++_i) { - func(); - } - auto _t3 = std::chrono::high_resolution_clock::now(); - return std::chrono::duration_cast(_t3 - _t2).count(); - } - )"; - compilation_result = Cpp::Process(timing_function.c_str()); + timeit::timeit() + { } void timeit::get_options(argparser& argpars) @@ -83,7 +68,7 @@ namespace xcpp .nargs(0); } - std::string timeit::inner(std::size_t number, const std::string& code) const + std::string timeit::inner(std::size_t number, const std::string& code, int exec_counter) const { static std::size_t counter = 0; // Ensure unique lambda names std::string unique_id = std::to_string(counter++); @@ -91,7 +76,8 @@ namespace xcpp timeit_code += "auto user_code_" + unique_id + " = []() {\n"; timeit_code += " " + code + "\n"; timeit_code += "};\n"; - timeit_code += "get_elapsed_time(" + std::to_string(number) + ", user_code_" + unique_id + ")\n"; + timeit_code += "get_elapsed_time_" + std::to_string(exec_counter) + "(" + std::to_string(number) + + ", user_code_" + unique_id + ")\n"; return timeit_code; } @@ -117,6 +103,7 @@ namespace xcpp void timeit::execute(std::string& line, std::string& cell) { + exec_counter++; argparser argpars("timeit", XEUS_CPP_VERSION, argparse::default_arguments::none); get_options(argpars); argpars.parse(line); @@ -155,6 +142,25 @@ namespace xcpp std::string err; bool hadError = false; + bool compilation_result = true; + compilation_result = Cpp::Process("#include \n#include \n"); + // Define the reusable timing function once + std::string timing_function = R"( + double get_elapsed_time_)" + + std::to_string(exec_counter) + + R"( (std::size_t num_iterations, void (*func)()) { + auto _t2 = std::chrono::high_resolution_clock::now(); + for (std::size_t _i = 0; _i < num_iterations; ++_i) { + func(); + } + auto _t3 = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(_t3 - _t2).count(); + return duration < 0 ? 0.0 : static_cast(duration); + } + )"; + + compilation_result = Cpp::Process(timing_function.c_str()); + try { if (number == 0) @@ -162,7 +168,7 @@ namespace xcpp for (std::size_t n = 0; n < 10; ++n) { number = std::pow(10, n); - std::string timeit_code = inner(number, code); + std::string timeit_code = inner(number, code, exec_counter); std::ostringstream buffer_out, buffer_err; std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); @@ -185,7 +191,7 @@ namespace xcpp double stdev = 0; for (std::size_t r = 0; r < static_cast(repeat); ++r) { - std::string timeit_code = inner(number, code); + std::string timeit_code = inner(number, code, exec_counter); std::ostringstream buffer_out, buffer_err; std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); diff --git a/src/xmagics/execution.hpp b/src/xmagics/execution.hpp index 383411ae..c96ddba3 100644 --- a/src/xmagics/execution.hpp +++ b/src/xmagics/execution.hpp @@ -23,7 +23,7 @@ namespace xcpp { public: - timeit(void* p); + timeit(); virtual void operator()(const std::string& line) override { @@ -39,11 +39,14 @@ namespace xcpp execute(cline, ccell); } + public: + + static int exec_counter; + private: - Cpp::TInterp_t m_interpreter; void get_options(argparser& argpars); - std::string inner(std::size_t number, const std::string& code) const; + std::string inner(std::size_t number, const std::string& code, int exec_counter) const; std::string _format_time(double timespan, std::size_t precision) const; void execute(std::string& line, std::string& cell); std::string wrap_code(const std::string& code) const; diff --git a/test/test_interpreter.cpp b/test/test_interpreter.cpp index fd18eb35..0d4e430e 100644 --- a/test/test_interpreter.cpp +++ b/test/test_interpreter.cpp @@ -16,11 +16,12 @@ #include "xeus-cpp/xoptions.hpp" #include "xeus-cpp/xeus_cpp_config.hpp" -#include "../src/xparser.hpp" -#include "../src/xsystem.hpp" +#include "../src/xinspect.hpp" +#include "../src/xmagics/execution.hpp" #include "../src/xmagics/os.hpp" #include "../src/xmagics/xassist.hpp" -#include "../src/xinspect.hpp" +#include "../src/xparser.hpp" +#include "../src/xsystem.hpp" #include @@ -1053,3 +1054,37 @@ TEST_SUITE("file") { infile.close(); } } + +TEST_SUITE("timeit") +{ + TEST_CASE("cell_check") + { + std::string line = "timeit"; + std::string cell = "std::cout << 1 << std::endl;"; + StreamRedirectRAII redirect(std::cout); + xcpp::timeit ti; + ti(line, cell); + std::string output = redirect.getCaptured(); + REQUIRE(output.find("mean +- std. dev. of") != std::string::npos); + } + + TEST_CASE("line_check") + { + std::string line = "timeit std::cout << 1 << std::endl;"; + StreamRedirectRAII redirect(std::cout); + xcpp::timeit ti; + ti(line); + std::string output = redirect.getCaptured(); + REQUIRE(output.find("mean +- std. dev. of") != std::string::npos); + } + + TEST_CASE("arg_check") + { + std::string line = "timeit -n 10 -r 1 -p 6 std::cout << 1 << std::endl;"; + StreamRedirectRAII redirect(std::cout); + xcpp::timeit ti; + ti(line); + std::string output = redirect.getCaptured(); + REQUIRE(output.find("mean +- std. dev. of 1 run, 10 loops each") != std::string::npos); + } +} From 4771adbe2a8fca4ab5268fb88b4945547dfd378e Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Fri, 18 Apr 2025 00:29:17 +0530 Subject: [PATCH 05/10] error handling --- src/xmagics/execution.cpp | 120 +++++++++++++++++++++----------------- test/test_interpreter.cpp | 11 ++++ 2 files changed, 76 insertions(+), 55 deletions(-) diff --git a/src/xmagics/execution.cpp b/src/xmagics/execution.cpp index 50255647..116f12b1 100644 --- a/src/xmagics/execution.cpp +++ b/src/xmagics/execution.cpp @@ -29,6 +29,7 @@ namespace xcpp { std::string out = Cpp::EndStdStreamCapture(); err = Cpp::EndStdStreamCapture(); + std::cout << out; } }; @@ -163,34 +164,43 @@ namespace xcpp try { - if (number == 0) - { - for (std::size_t n = 0; n < 10; ++n) - { - number = std::pow(10, n); - std::string timeit_code = inner(number, code, exec_counter); - std::ostringstream buffer_out, buffer_err; - std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); - std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); - StreamRedirectRAII R(err); - auto res_ptr = Cpp::Evaluate(timeit_code.c_str(), &hadError); - std::cout.rdbuf(old_cout); - std::cerr.rdbuf(old_cerr); - output = std::to_string(res_ptr); - err += buffer_err.str(); - double elapsed_time = std::stod(output) * 1e-6; - if (elapsed_time >= 0.2) - { - break; - } - } - } + StreamRedirectRAII R(err); + std::ostringstream buffer_out, buffer_err; + std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); + std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); + compilation_result = Cpp::Declare(code.c_str()); + std::cout.rdbuf(old_cout); + std::cerr.rdbuf(old_cerr); + } + catch (std::exception& e) + { + errorlevel = 1; + ename = "Standard Exception: "; + evalue = e.what(); + return; + } + catch (...) + { + errorlevel = 1; + ename = "Error: "; + return; + } + + if (compilation_result) + { + errorlevel = 1; + ename = "Error: "; + evalue = "Compilation error! " + err; + std::cerr << err; + return; + } - std::vector all_runs; - double mean = 0; - double stdev = 0; - for (std::size_t r = 0; r < static_cast(repeat); ++r) + + if (number == 0) + { + for (std::size_t n = 0; n < 10; ++n) { + number = std::pow(10, n); std::string timeit_code = inner(number, code, exec_counter); std::ostringstream buffer_out, buffer_err; std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); @@ -202,41 +212,41 @@ namespace xcpp output = std::to_string(res_ptr); err += buffer_err.str(); double elapsed_time = std::stod(output) * 1e-6; - all_runs.push_back(elapsed_time / number); - mean += all_runs.back(); - } - mean /= repeat; - for (std::size_t r = 0; r < static_cast(repeat); ++r) - { - stdev += (all_runs[r] - mean) * (all_runs[r] - mean); + if (elapsed_time >= 0.2) + { + break; + } } - stdev = std::sqrt(stdev / repeat); - - std::cout << _format_time(mean, precision) << " +- " << _format_time(stdev, precision); - std::cout << " per loop (mean +- std. dev. of " << repeat << " run" - << ((repeat == 1) ? ", " : "s "); - std::cout << number << " loop" << ((number == 1) ? "" : "s") << " each)" << std::endl; } - catch (std::exception& e) + + std::vector all_runs; + double mean = 0; + double stdev = 0; + for (std::size_t r = 0; r < static_cast(repeat); ++r) { - errorlevel = 1; - ename = "Standard Exception: "; - evalue = e.what(); + std::string timeit_code = inner(number, code, exec_counter); + std::ostringstream buffer_out, buffer_err; + std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf()); + std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf()); + StreamRedirectRAII R(err); + auto res_ptr = Cpp::Evaluate(timeit_code.c_str(), &hadError); + std::cout.rdbuf(old_cout); + std::cerr.rdbuf(old_cerr); + output = std::to_string(res_ptr); + err += buffer_err.str(); + double elapsed_time = std::stod(output) * 1e-6; + all_runs.push_back(elapsed_time / number); + mean += all_runs.back(); } - catch (...) + mean /= repeat; + for (std::size_t r = 0; r < static_cast(repeat); ++r) { - errorlevel = 1; - ename = "Error: "; + stdev += (all_runs[r] - mean) * (all_runs[r] - mean); } + stdev = std::sqrt(stdev / repeat); - if (hadError) - { - errorlevel = 1; - ename = "Error: "; - evalue = "Compilation error! " + err; - std::cerr << err; - std::cerr << "Error: " << evalue << std::endl; - std::cerr << "Error: " << err << std::endl; - } + std::cout << _format_time(mean, precision) << " +- " << _format_time(stdev, precision); + std::cout << " per loop (mean +- std. dev. of " << repeat << " run" << ((repeat == 1) ? ", " : "s "); + std::cout << number << " loop" << ((number == 1) ? "" : "s") << " each)" << std::endl; } } \ No newline at end of file diff --git a/test/test_interpreter.cpp b/test/test_interpreter.cpp index 0d4e430e..5327ce1e 100644 --- a/test/test_interpreter.cpp +++ b/test/test_interpreter.cpp @@ -1087,4 +1087,15 @@ TEST_SUITE("timeit") std::string output = redirect.getCaptured(); REQUIRE(output.find("mean +- std. dev. of 1 run, 10 loops each") != std::string::npos); } + + TEST_CASE("fail_check") + { + std::string line = "timeit"; + std::string cell = "int x = "; + StreamRedirectRAII redirect(std::cerr); + xcpp::timeit ti; + ti(line, cell); + std::string output = redirect.getCaptured(); + REQUIRE(output.find("expected expression") != std::string::npos); + } } From 6d08824d674a514e6a38cc70490f127ccea2ea05 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Fri, 18 Apr 2025 10:46:39 +0530 Subject: [PATCH 06/10] removed timeit def constr --- src/xmagics/execution.cpp | 4 ---- src/xmagics/execution.hpp | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/xmagics/execution.cpp b/src/xmagics/execution.cpp index 116f12b1..d6893348 100644 --- a/src/xmagics/execution.cpp +++ b/src/xmagics/execution.cpp @@ -35,10 +35,6 @@ namespace xcpp int timeit::exec_counter = 0; - timeit::timeit() - { - } - void timeit::get_options(argparser& argpars) { argpars.add_description("Time execution of a C++ statement or expression"); diff --git a/src/xmagics/execution.hpp b/src/xmagics/execution.hpp index c96ddba3..1d07567a 100644 --- a/src/xmagics/execution.hpp +++ b/src/xmagics/execution.hpp @@ -23,8 +23,7 @@ namespace xcpp { public: - timeit(); - + XEUS_CPP_API virtual void operator()(const std::string& line) override { std::string cline = line; @@ -32,6 +31,7 @@ namespace xcpp execute(cline, cell); } + XEUS_CPP_API virtual void operator()(const std::string& line, const std::string& cell) override { std::string cline = line; From 08621ece03fe00eedff6570e007d71e5687518d9 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Sat, 19 Apr 2025 13:06:08 +0530 Subject: [PATCH 07/10] definition shifted to cpp file --- src/xmagics/execution.cpp | 14 ++++++++++++++ src/xmagics/execution.hpp | 17 ++--------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/xmagics/execution.cpp b/src/xmagics/execution.cpp index d6893348..fec2c197 100644 --- a/src/xmagics/execution.cpp +++ b/src/xmagics/execution.cpp @@ -35,6 +35,20 @@ namespace xcpp int timeit::exec_counter = 0; + void timeit::operator()(const std::string& line) + { + std::string cline = line; + std::string ccell = ""; + execute(cline, ccell); + } + + void timeit::operator()(const std::string& line, const std::string& cell) + { + std::string cline = line; + std::string ccell = cell; + execute(cline, ccell); + } + void timeit::get_options(argparser& argpars) { argpars.add_description("Time execution of a C++ statement or expression"); diff --git a/src/xmagics/execution.hpp b/src/xmagics/execution.hpp index 1d07567a..f2708f75 100644 --- a/src/xmagics/execution.hpp +++ b/src/xmagics/execution.hpp @@ -12,8 +12,6 @@ #include #include -#include "clang/Interpreter/CppInterOp.h" - #include "xeus-cpp/xmagics.hpp" #include "xeus-cpp/xoptions.hpp" @@ -24,20 +22,10 @@ namespace xcpp public: XEUS_CPP_API - virtual void operator()(const std::string& line) override - { - std::string cline = line; - std::string cell = ""; - execute(cline, cell); - } + virtual void operator()(const std::string& line) override; XEUS_CPP_API - virtual void operator()(const std::string& line, const std::string& cell) override - { - std::string cline = line; - std::string ccell = cell; - execute(cline, ccell); - } + virtual void operator()(const std::string& line, const std::string& cell) override; public: @@ -49,7 +37,6 @@ namespace xcpp std::string inner(std::size_t number, const std::string& code, int exec_counter) const; std::string _format_time(double timespan, std::size_t precision) const; void execute(std::string& line, std::string& cell); - std::string wrap_code(const std::string& code) const; }; } #endif \ No newline at end of file From 989a66e5095d76ea4bb9163c83bbc3a83373ecad Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Sat, 19 Apr 2025 13:26:12 +0530 Subject: [PATCH 08/10] removed iostream from timeit code --- src/xmagics/execution.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xmagics/execution.cpp b/src/xmagics/execution.cpp index fec2c197..c586399a 100644 --- a/src/xmagics/execution.cpp +++ b/src/xmagics/execution.cpp @@ -154,7 +154,7 @@ namespace xcpp bool hadError = false; bool compilation_result = true; - compilation_result = Cpp::Process("#include \n#include \n"); + compilation_result = Cpp::Process("#include \n"); // Define the reusable timing function once std::string timing_function = R"( double get_elapsed_time_)" From aebc3e799969546d1e1688e69dd8225c943217a7 Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Sat, 19 Apr 2025 13:32:08 +0530 Subject: [PATCH 09/10] removed chrono lib --- src/xmagics/execution.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xmagics/execution.cpp b/src/xmagics/execution.cpp index c586399a..91f68bf5 100644 --- a/src/xmagics/execution.cpp +++ b/src/xmagics/execution.cpp @@ -154,7 +154,7 @@ namespace xcpp bool hadError = false; bool compilation_result = true; - compilation_result = Cpp::Process("#include \n"); + // compilation_result = Cpp::Process("#include \n"); // Define the reusable timing function once std::string timing_function = R"( double get_elapsed_time_)" From 265fba83e9a8ce97e79d01c2fa868ac5e269f2ba Mon Sep 17 00:00:00 2001 From: kr-2003 Date: Sat, 19 Apr 2025 13:35:34 +0530 Subject: [PATCH 10/10] added chrono lib --- src/xmagics/execution.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xmagics/execution.cpp b/src/xmagics/execution.cpp index 91f68bf5..c586399a 100644 --- a/src/xmagics/execution.cpp +++ b/src/xmagics/execution.cpp @@ -154,7 +154,7 @@ namespace xcpp bool hadError = false; bool compilation_result = true; - // compilation_result = Cpp::Process("#include \n"); + compilation_result = Cpp::Process("#include \n"); // Define the reusable timing function once std::string timing_function = R"( double get_elapsed_time_)"