| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 47620a6 commit fe98e2f
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,16 +6,12 @@ | |||
| 6 | 6 | # | |
| 7 | 7 | ||
| 8 | 8 | import time | |
| 9 | - import sys | ||
| 10 | 9 | import multiprocessing | |
| 11 | 10 | import threading | |
| 12 | 11 | import queue | |
| 13 | 12 | import gc | |
| 14 | 13 | ||
| 15 | - if sys.platform == 'win32': | ||
| 16 | - _timer = time.clock | ||
| 17 | - else: | ||
| 18 | - _timer = time.time | ||
| 14 | + _timer = time.perf_counter | ||
| 19 | 15 | ||
| 20 | 16 | delta = 1 | |
| 21 | 17 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -149,9 +149,9 @@ def recolorize(self): | |||
| 149 | 149 | self.stop_colorizing = False | |
| 150 | 150 | self.colorizing = True | |
| 151 | 151 | if DEBUG: print("colorizing...") | |
| 152 | - t0 = time.clock() | ||
| 152 | + t0 = time.perf_counter() | ||
| 153 | 153 | self.recolorize_main() | |
| 154 | - t1 = time.clock() | ||
| 154 | + t1 = time.perf_counter() | ||
| 155 | 155 | if DEBUG: print("%.3f seconds" % (t1-t0)) | |
| 156 | 156 | finally: | |
| 157 | 157 | self.colorizing = False | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,26 +83,6 @@ def runctx(statement, globals, locals, filename=None, sort=-1): | |||
| 83 | 83 | else: | |
| 84 | 84 | return prof.print_stats(sort) | |
| 85 | 85 | ||
| 86 | - if hasattr(os, "times"): | ||
| 87 | - def _get_time_times(timer=os.times): | ||
| 88 | - t = timer() | ||
| 89 | - return t[0] + t[1] | ||
| 90 | - | ||
| 91 | - # Using getrusage(3) is better than clock(3) if available: | ||
| 92 | - # on some systems (e.g. FreeBSD), getrusage has a higher resolution | ||
| 93 | - # Furthermore, on a POSIX system, returns microseconds, which | ||
| 94 | - # wrap around after 36min. | ||
| 95 | - _has_res = 0 | ||
| 96 | - try: | ||
| 97 | - import resource | ||
| 98 | - resgetrusage = lambda: resource.getrusage(resource.RUSAGE_SELF) | ||
| 99 | - def _get_time_resource(timer=resgetrusage): | ||
| 100 | - t = timer() | ||
| 101 | - return t[0] + t[1] | ||
| 102 | - _has_res = 1 | ||
| 103 | - except ImportError: | ||
| 104 | - pass | ||
| 105 | - | ||
| 106 | 86 | class Profile: | |
| 107 | 87 | """Profiler class. | |
| 108 | 88 | ||
@@ -155,20 +135,8 @@ def __init__(self, timer=None, bias=None): | |||
| 155 | 135 | self.bias = bias # Materialize in local dict for lookup speed. | |
| 156 | 136 | ||
| 157 | 137 | if not timer: | |
| 158 | - if _has_res: | ||
| 159 | - self.timer = resgetrusage | ||
| 160 | - self.dispatcher = self.trace_dispatch | ||
| 161 | - self.get_time = _get_time_resource | ||
| 162 | - elif hasattr(time, 'clock'): | ||
| 163 | - self.timer = self.get_time = time.clock | ||
| 164 | - self.dispatcher = self.trace_dispatch_i | ||
| 165 | - elif hasattr(os, 'times'): | ||
| 166 | - self.timer = os.times | ||
| 167 | - self.dispatcher = self.trace_dispatch | ||
| 168 | - self.get_time = _get_time_times | ||
| 169 | - else: | ||
| 170 | - self.timer = self.get_time = time.time | ||
| 171 | - self.dispatcher = self.trace_dispatch_i | ||
| 138 | + self.timer = self.get_time = time.process_time | ||
| 139 | + self.dispatcher = self.trace_dispatch_i | ||
| 172 | 140 | else: | |
| 173 | 141 | self.timer = timer | |
| 174 | 142 | t = self.timer() # test out timer function | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,13 +9,13 @@ def main(): | |||
| 9 | 9 | timefunc(10, p.findall, s) | |
| 10 | 10 | ||
| 11 | 11 | def timefunc(n, func, *args, **kw): | |
| 12 | - t0 = time.clock() | ||
| 12 | + t0 = time.perf_counter() | ||
| 13 | 13 | try: | |
| 14 | 14 | for i in range(n): | |
| 15 | 15 | result = func(*args, **kw) | |
| 16 | 16 | return result | |
| 17 | 17 | finally: | |
| 18 | - t1 = time.clock() | ||
| 18 | + t1 = time.perf_counter() | ||
| 19 | 19 | if n > 1: | |
| 20 | 20 | print(n, "times", end=' ') | |
| 21 | 21 | print(func.__name__, "%.3f" % (t1-t0), "CPU seconds") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,9 +57,9 @@ def flush(): | |||
| 57 | 57 | sys.stdout.flush() | |
| 58 | 58 | ||
| 59 | 59 | def doit(L): | |
| 60 | - t0 = time.clock() | ||
| 60 | + t0 = time.perf_counter() | ||
| 61 | 61 | L.sort() | |
| 62 | - t1 = time.clock() | ||
| 62 | + t1 = time.perf_counter() | ||
| 63 | 63 | print("%6.2f" % (t1-t0), end=' ') | |
| 64 | 64 | flush() | |
| 65 | 65 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,8 +15,8 @@ | |||
| 15 | 15 | -n/--number N: how many times to execute 'statement' (default: see below) | |
| 16 | 16 | -r/--repeat N: how many times to repeat the timer (default 3) | |
| 17 | 17 | -s/--setup S: statement to be executed once initially (default 'pass') | |
| 18 | - -t/--time: use time.time() (default on Unix) | ||
| 19 | - -c/--clock: use time.clock() (default on Windows) | ||
| 18 | + -t/--time: use time.time() | ||
| 19 | + -c/--clock: use time.clock() | ||
| 20 | 20 | -v/--verbose: print raw timing results; repeat for more digits precision | |
| 21 | 21 | -h/--help: print this usage message and exit | |
| 22 | 22 | --: separate options from statement, use when statement starts with - | |
@@ -66,13 +66,7 @@ | |||
| 66 | 66 | dummy_src_name = "<timeit-src>" | |
| 67 | 67 | default_number = 1000000 | |
| 68 | 68 | default_repeat = 3 | |
| 69 | - | ||
| 70 | - if sys.platform == "win32": | ||
| 71 | - # On Windows, the best timer is time.clock() | ||
| 72 | - default_timer = time.clock | ||
| 73 | - else: | ||
| 74 | - # On most other platforms the best timer is time.time() | ||
| 75 | - default_timer = time.time | ||
| 69 | + default_timer = time.perf_counter | ||
| 76 | 70 | ||
| 77 | 71 | # Don't change the indentation of the template; the reindent() calls | |
| 78 | 72 | # in Timer.__init__() depend on setup being indented 4 spaces and stmt | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,11 +73,15 @@ | |||
| 73 | 73 | ||
| 74 | 74 | # Timer types | |
| 75 | 75 | TIMER_TIME_TIME = 'time.time' | |
| 76 | + TIMER_TIME_PROCESS_TIME = 'time.process_time' | ||
| 77 | + TIMER_TIME_PERF_COUNTER = 'time.perf_counter' | ||
| 76 | 78 | TIMER_TIME_CLOCK = 'time.clock' | |
| 77 | 79 | TIMER_SYSTIMES_PROCESSTIME = 'systimes.processtime' | |
| 78 | 80 | ||
| 79 | 81 | # Choose platform default timer | |
| 80 | - if sys.platform[:3] == 'win': | ||
| 82 | + if hasattr(time, 'perf_counter'): | ||
| 83 | + TIMER_PLATFORM_DEFAULT = TIMER_TIME_PERF_COUNTER | ||
| 84 | + elif sys.platform[:3] == 'win': | ||
| 81 | 85 | # On WinXP this has 2.5ms resolution | |
| 82 | 86 | TIMER_PLATFORM_DEFAULT = TIMER_TIME_CLOCK | |
| 83 | 87 | else: | |
@@ -93,6 +97,10 @@ def get_timer(timertype): | |||
| 93 | 97 | ||
| 94 | 98 | if timertype == TIMER_TIME_TIME: | |
| 95 | 99 | return time.time | |
| 100 | + elif timertype == TIMER_TIME_PROCESS_TIME: | ||
| 101 | + return time.process_time | ||
| 102 | + elif timertype == TIMER_TIME_PERF_COUNTER: | ||
| 103 | + return time.perf_counter | ||
| 96 | 104 | elif timertype == TIMER_TIME_CLOCK: | |
| 97 | 105 | return time.clock | |
| 98 | 106 | elif timertype == TIMER_SYSTIMES_PROCESSTIME: | |
@@ -866,7 +874,18 @@ def main(self): | |||
| 866 | 874 | print('* using timer: systimes.processtime (%s)' % \ | |
| 867 | 875 | systimes.SYSTIMES_IMPLEMENTATION) | |
| 868 | 876 | else: | |
| 877 | + # Check that the clock function does exist | ||
| 878 | + try: | ||
| 879 | + get_timer(timer) | ||
| 880 | + except TypeError: | ||
| 881 | + print("* Error: Unknown timer: %s" % timer) | ||
| 882 | + return | ||
| 883 | + | ||
| 869 | 884 | print('* using timer: %s' % timer) | |
| 885 | + if hasattr(time, 'get_clock_info'): | ||
| 886 | + info = time.get_clock_info(timer[5:]) | ||
| 887 | + print('* timer: resolution=%s, implementation=%s' | ||
| 888 | + % (info.resolution, info.implementation)) | ||
| 870 | 889 | ||
| 871 | 890 | print() | |
| 872 | 891 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments