Guido van Rossum | 58dd035 | 1992-12-17 17:32:35 +0000 | [diff] [blame] | 1 | # Simple interface to report execution times of program fragments. |
| 2 | # Call TSTART() to reset the timer, TSTOP(...) to report times. |
| 3 | |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 4 | import sys, os, time |
| 5 | |
| 6 | def TSTART(): |
| 7 | global t0, t1 |
| 8 | u, s, cu, cs = os.times() |
Guido van Rossum | e69d3fe | 1994-02-17 12:36:58 +0000 | [diff] [blame] | 9 | t0 = u+cu, s+cs, time.time() |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 10 | |
| 11 | def TSTOP(*label): |
| 12 | global t0, t1 |
| 13 | u, s, cu, cs = os.times() |
Guido van Rossum | e69d3fe | 1994-02-17 12:36:58 +0000 | [diff] [blame] | 14 | t1 = u+cu, s+cs, time.time() |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 15 | tt = [] |
| 16 | for i in range(3): |
| 17 | tt.append(t1[i] - t0[i]) |
| 18 | [u, s, r] = tt |
| 19 | msg = '' |
| 20 | for x in label: msg = msg + (x + ' ') |
Guido van Rossum | e69d3fe | 1994-02-17 12:36:58 +0000 | [diff] [blame] | 21 | msg = msg + `u` + ' user, ' + `s` + ' sys, ' + `r` + ' real\n' |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 22 | sys.stderr.write(msg) |