def test_TimerStatsAll(self): t = SmartTimer() for i in range(5): t.tic('loop ' + str(i)) t.sleep(0.2) t.toc() stats = t.stats() print(stats) self.assertAlmostEqual(0.2, stats.min[0], 1) self.assertAlmostEqual(0.2, stats.max[0], 1) self.assertAlmostEqual(0.2, stats.avg[0], 1)
def test_WithStatement(self): t = SmartTimer() with t: t.sleep(1) self.assertEqual(len(t.labels), 1) self.assertEqual(len(t.active_labels), 0) self.assertEqual(len(t.seconds), 1) self.assertEqual(len(t.minutes), 1) self.assertEqual(len(t.times), 1) self.assertIn('', t.times) self.assertAlmostEqual(t.walltime()[0], t.seconds[0], 1)
def test_CascadeScheme(self): t = SmartTimer() t.tic('A') t.sleep(0.5) t.toc() t.sleep(0.5) t.toc('B') self.assertEqual(len(t.labels), 2) self.assertEqual(len(t.active_labels), 0) self.assertEqual(len(t.seconds), 2) self.assertEqual(len(t.minutes), 2) self.assertEqual(len(t.times), 2) self.assertIn('A', t.times) self.assertIn('B', t.times) self.assertEqual(t.walltime()[0], t.seconds[1])
def test_ConsecutiveScheme(self): t = SmartTimer() t.tic('A') t.sleep(0.5) t.toc() t.tic('B') t.sleep(0.5) t.toc() self.assertEqual(len(t.labels), 2) self.assertEqual(len(t.active_labels), 0) self.assertEqual(len(t.seconds), 2) self.assertEqual(len(t.minutes), 2) self.assertEqual(len(t.times), 2) self.assertIn('A', t.times) self.assertIn('B', t.times) self.assertAlmostEqual(t.walltime()[0], sum(t.seconds), 1)
def test_LabelPairedScheme(self): t = SmartTimer() t.tic('A') t.sleep(0.5) t.tic('B') t.sleep(0.5) t.toc('A') t.sleep(0.5) t.toc('B') self.assertEqual(len(t.labels), 2) self.assertEqual(len(t.active_labels), 0) self.assertEqual(len(t.seconds), 2) self.assertEqual(len(t.minutes), 2) self.assertEqual(len(t.times), 2) self.assertIn('A', t.times) self.assertIn('B', t.times)
# Create a timer instance named 'Example' t = SmartTimer("Example") # Print clock details t.tic("info") t.print_info() t.toc() # Measure iterations in a loop t.tic("loop") for i in range(10): t.tic("iter " + str(i)) sum(range(1000000)) t.toc() t.toc() t.tic("sleep") t.sleep(2) t.toc() # Write times to file 'Example.txt' t.to_file() print(t.times) print(t["info"]) print(t.walltime()) print(t) # Print stats only for labels with keyword 'iter' print(t.stats("iter"))