Skip to content

Commit c3a0565

Browse files
author
kr-2003
committed
added tests for timeit
1 parent d2703cd commit c3a0565

File tree

4 files changed

+73
-32
lines changed

4 files changed

+73
-32
lines changed

src/xinterpreter.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,7 @@ __get_cxx_version ()
399399
// timeit(&m_interpreter));
400400
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("python", pythonexec());
401401
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("file", writefile());
402-
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic(
403-
"timeit",
404-
timeit(Cpp::GetInterpreter())
405-
);
402+
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("timeit", timeit());
406403
#ifndef EMSCRIPTEN
407404
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("xassist", xassist());
408405
#endif

src/xmagics/execution.cpp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,10 @@ namespace xcpp
3232
}
3333
};
3434

35-
timeit::timeit(Cpp::TInterp_t p)
36-
: m_interpreter(p)
37-
{
38-
bool compilation_result = true;
39-
std::string err;
40-
compilation_result = Cpp::Process("#include <chrono>\n#include <iostream>\n");
35+
int timeit::exec_counter = 0;
4136

42-
// Define the reusable timing function once
43-
std::string timing_function = R"(
44-
double get_elapsed_time(std::size_t num_iterations, void (*func)()) {
45-
auto _t2 = std::chrono::high_resolution_clock::now();
46-
for (std::size_t _i = 0; _i < num_iterations; ++_i) {
47-
func();
48-
}
49-
auto _t3 = std::chrono::high_resolution_clock::now();
50-
return std::chrono::duration_cast<std::chrono::microseconds>(_t3 - _t2).count();
51-
}
52-
)";
53-
compilation_result = Cpp::Process(timing_function.c_str());
37+
timeit::timeit()
38+
{
5439
}
5540

5641
void timeit::get_options(argparser& argpars)
@@ -83,15 +68,16 @@ namespace xcpp
8368
.nargs(0);
8469
}
8570

86-
std::string timeit::inner(std::size_t number, const std::string& code) const
71+
std::string timeit::inner(std::size_t number, const std::string& code, int exec_counter) const
8772
{
8873
static std::size_t counter = 0; // Ensure unique lambda names
8974
std::string unique_id = std::to_string(counter++);
9075
std::string timeit_code = "";
9176
timeit_code += "auto user_code_" + unique_id + " = []() {\n";
9277
timeit_code += " " + code + "\n";
9378
timeit_code += "};\n";
94-
timeit_code += "get_elapsed_time(" + std::to_string(number) + ", user_code_" + unique_id + ")\n";
79+
timeit_code += "get_elapsed_time_" + std::to_string(exec_counter) + "(" + std::to_string(number)
80+
+ ", user_code_" + unique_id + ")\n";
9581
return timeit_code;
9682
}
9783

@@ -117,6 +103,7 @@ namespace xcpp
117103

118104
void timeit::execute(std::string& line, std::string& cell)
119105
{
106+
exec_counter++;
120107
argparser argpars("timeit", XEUS_CPP_VERSION, argparse::default_arguments::none);
121108
get_options(argpars);
122109
argpars.parse(line);
@@ -155,14 +142,33 @@ namespace xcpp
155142
std::string err;
156143
bool hadError = false;
157144

145+
bool compilation_result = true;
146+
compilation_result = Cpp::Process("#include <chrono>\n#include <iostream>\n");
147+
// Define the reusable timing function once
148+
std::string timing_function = R"(
149+
double get_elapsed_time_)"
150+
+ std::to_string(exec_counter)
151+
+ R"( (std::size_t num_iterations, void (*func)()) {
152+
auto _t2 = std::chrono::high_resolution_clock::now();
153+
for (std::size_t _i = 0; _i < num_iterations; ++_i) {
154+
func();
155+
}
156+
auto _t3 = std::chrono::high_resolution_clock::now();
157+
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(_t3 - _t2).count();
158+
return duration < 0 ? 0.0 : static_cast<double>(duration);
159+
}
160+
)";
161+
162+
compilation_result = Cpp::Process(timing_function.c_str());
163+
158164
try
159165
{
160166
if (number == 0)
161167
{
162168
for (std::size_t n = 0; n < 10; ++n)
163169
{
164170
number = std::pow(10, n);
165-
std::string timeit_code = inner(number, code);
171+
std::string timeit_code = inner(number, code, exec_counter);
166172
std::ostringstream buffer_out, buffer_err;
167173
std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf());
168174
std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf());
@@ -185,7 +191,7 @@ namespace xcpp
185191
double stdev = 0;
186192
for (std::size_t r = 0; r < static_cast<std::size_t>(repeat); ++r)
187193
{
188-
std::string timeit_code = inner(number, code);
194+
std::string timeit_code = inner(number, code, exec_counter);
189195
std::ostringstream buffer_out, buffer_err;
190196
std::streambuf* old_cout = std::cout.rdbuf(buffer_out.rdbuf());
191197
std::streambuf* old_cerr = std::cerr.rdbuf(buffer_err.rdbuf());

src/xmagics/execution.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace xcpp
2323
{
2424
public:
2525

26-
timeit(void* p);
26+
timeit();
2727

2828
virtual void operator()(const std::string& line) override
2929
{
@@ -39,11 +39,14 @@ namespace xcpp
3939
execute(cline, ccell);
4040
}
4141

42+
public:
43+
44+
static int exec_counter;
45+
4246
private:
4347

44-
Cpp::TInterp_t m_interpreter;
4548
void get_options(argparser& argpars);
46-
std::string inner(std::size_t number, const std::string& code) const;
49+
std::string inner(std::size_t number, const std::string& code, int exec_counter) const;
4750
std::string _format_time(double timespan, std::size_t precision) const;
4851
void execute(std::string& line, std::string& cell);
4952
std::string wrap_code(const std::string& code) const;

test/test_interpreter.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
#include "xeus-cpp/xoptions.hpp"
1717
#include "xeus-cpp/xeus_cpp_config.hpp"
1818

19-
#include "../src/xparser.hpp"
20-
#include "../src/xsystem.hpp"
19+
#include "../src/xinspect.hpp"
20+
#include "../src/xmagics/execution.hpp"
2121
#include "../src/xmagics/os.hpp"
2222
#include "../src/xmagics/xassist.hpp"
23-
#include "../src/xinspect.hpp"
23+
#include "../src/xparser.hpp"
24+
#include "../src/xsystem.hpp"
2425

2526

2627
#include <iostream>
@@ -1053,3 +1054,37 @@ TEST_SUITE("file") {
10531054
infile.close();
10541055
}
10551056
}
1057+
1058+
TEST_SUITE("timeit")
1059+
{
1060+
TEST_CASE("cell_check")
1061+
{
1062+
std::string line = "timeit";
1063+
std::string cell = "std::cout << 1 << std::endl;";
1064+
StreamRedirectRAII redirect(std::cout);
1065+
xcpp::timeit ti;
1066+
ti(line, cell);
1067+
std::string output = redirect.getCaptured();
1068+
REQUIRE(output.find("mean +- std. dev. of") != std::string::npos);
1069+
}
1070+
1071+
TEST_CASE("line_check")
1072+
{
1073+
std::string line = "timeit std::cout << 1 << std::endl;";
1074+
StreamRedirectRAII redirect(std::cout);
1075+
xcpp::timeit ti;
1076+
ti(line);
1077+
std::string output = redirect.getCaptured();
1078+
REQUIRE(output.find("mean +- std. dev. of") != std::string::npos);
1079+
}
1080+
1081+
TEST_CASE("arg_check")
1082+
{
1083+
std::string line = "timeit -n 10 -r 1 -p 6 std::cout << 1 << std::endl;";
1084+
StreamRedirectRAII redirect(std::cout);
1085+
xcpp::timeit ti;
1086+
ti(line);
1087+
std::string output = redirect.getCaptured();
1088+
REQUIRE(output.find("mean +- std. dev. of 1 run, 10 loops each") != std::string::npos);
1089+
}
1090+
}

0 commit comments

Comments
 (0)