@@ -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_" "  = []() {\n " 
9277        timeit_code += "    " " \n " 
9378        timeit_code += " };\n " 
94-         timeit_code += " get_elapsed_time(" std::to_string (number) + " , user_code_" " )\n " 
79+         timeit_code += " get_elapsed_time_" std::to_string (exec_counter) + " (" std::to_string (number)
80+                        + " , user_code_" " )\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" 
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 ());
0 commit comments