def test_elapsed_time(self): stopwatch = StopWatch() start_time = stopwatch.start() elapsed_time_ongoing = stopwatch.elapsed_time elapsed_time_final = stopwatch.stop() self.assertTrue(elapsed_time_ongoing > 0) self.assertTrue(elapsed_time_ongoing < elapsed_time_final)
def test_stopwatch_checkpoint(self): stopwatch = StopWatch() start_time = stopwatch.start() checkpoint_1 = stopwatch.checkpoint() checkpoint_2 = stopwatch.checkpoint() elapsed_time = stopwatch.stop() self.assertTrue(checkpoint_1 + checkpoint_2 <= elapsed_time)
def test_stopwatch(self): stopwatch = StopWatch() start_time = stopwatch.start() self.assertTrue(start_time > 0) elapsed_time = stopwatch.stop() self.assertTrue(elapsed_time > 0) self.assertEqual(elapsed_time, stopwatch.elapsed_time) self.assertEqual( elapsed_time, stopwatch.stop_time - stopwatch.start_time )
def run(self): """Run a measurement. Returns: list -- List of results. """ print(f"> {self.name}") if self.compile_cmd: cmd_compile = delegator.run(self.compile_cmd) if self.debug or cmd_compile.return_code is not 0: logging.info(cmd_compile.out) print( f"Version: {delegator.run(self.version_cmd).out.splitlines()[0]}") times = [] count = 0 while count < 10: count += 1 # Measure execution time watch = StopWatch() watch.start() cmd_run = delegator.run(self.run_cmd) watch.stop() # Convert the measurement into milliseconds times.append(int(watch.elapsed_time * 1000)) print(f"Speed (all): {'ms, '.join(map(str, times))}ms") print(f"Speed (best): {min(times)}ms") print(f"Speed (worst): {max(times)}ms") print(f"Speed (median): {statistics.median(times)}ms") # Strip output from new line result = cmd_run.out.strip() print(f"Result: {result}") # Calculate accuracy if not self.debug: accuracy = self.diff_letters(f"{math.pi:.{len(result)-2}f}", result) print(f"Accuracy: {accuracy:.2%}") else: accuracy = 0.0000 print() # new line self.results["median"] = statistics.median(times) self.results["best"] = min(times) self.results["worst"] = max(times) self.results["accuracy"] = f"{accuracy*100:.4}" return self.results
def awesome_sum(a, b): return a + b @stopwatch def awesome_mul(a, b): return a * b @stopwatch def awesome_print(): print 'Hello Niki!' print awesome_sum(5, 10) print awesome_mul(5, 10) awesome_print() watch = StopWatch() watch.start() for i in range(100000): pass print ('Time is running out.. {0} sec.'.format(watch.elapsed_time)) for i in range(10000000): pass watch.stop() print ('Time spent in range: {0} sec.'.format(watch.elapsed_time)) with stopwatchcm(callback=stopwatch_sum_cb_w_cm): c = 5 * 10 watch = StopWatch() watch.start() for i in range(10000000):
from lauda import StopWatch, stopwatch def stopwatch_sum_cb(watch, function): print ('Time spent inside {0} is {1} sec.'.format(function, watch.elapsed_time)) @stopwatch(callback=stopwatch_sum_cb) def awesome_sum(a, b): return a + b @stopwatch def awesome_mul(a, b): return a * b @stopwatch def awesome_print(): print 'Hello Niki!' print awesome_sum(5, 10) print awesome_mul(5, 10) awesome_print() watch = StopWatch() watch.start() for i in range(10000000): pass watch.stop() print ('Time spent in range: {0} sec.'.format(watch.elapsed_time))