Skip to content

Commit 43a87f7

Browse files
authored
Replace time.time() with time.perf_counter() (#1777)
time.perf_counter() is guaranteed to use the highest-resolution clock available, and is monotonically increasing, neither of which are true for time.time(). Thanks @matthiask for the suggestion!
1 parent e7575e8 commit 43a87f7

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

debug_toolbar/management/commands/debugsqlshell.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from time import time
1+
from time import perf_counter
22

33
import sqlparse
44
from django.core.management.commands.shell import Command
@@ -19,12 +19,12 @@
1919

2020
class PrintQueryWrapper(base_module.CursorDebugWrapper):
2121
def execute(self, sql, params=()):
22-
start_time = time()
22+
start_time = perf_counter()
2323
try:
2424
return self.cursor.execute(sql, params)
2525
finally:
2626
raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
27-
end_time = time()
27+
end_time = perf_counter()
2828
duration = (end_time - start_time) * 1000
2929
formatted_sql = sqlparse.format(raw_sql, reindent=True)
3030
print(f"{formatted_sql} [{duration:.2f}ms]")

debug_toolbar/panels/cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import functools
2-
import time
2+
from time import perf_counter
33

44
from asgiref.local import Local
55
from django.conf import settings
@@ -148,9 +148,9 @@ def _record_call(self, cache, name, original_method, args, kwargs):
148148
# the course of this call, and then reset it back afterward.
149149
cache._djdt_panel = None
150150
try:
151-
t = time.time()
151+
start_time = perf_counter()
152152
value = original_method(*args, **kwargs)
153-
t = time.time() - t
153+
t = perf_counter() - start_time
154154
finally:
155155
cache._djdt_panel = self
156156

debug_toolbar/panels/sql/tracking.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import contextvars
22
import datetime
33
import json
4-
from time import time
4+
from time import perf_counter
55

66
import django.test.testcases
77
from django.db.backends.utils import CursorWrapper
@@ -162,11 +162,11 @@ def _record(self, method, sql, params):
162162
conn = self.db.connection
163163
initial_conn_status = conn.info.transaction_status
164164

165-
start_time = time()
165+
start_time = perf_counter()
166166
try:
167167
return method(sql, params)
168168
finally:
169-
stop_time = time()
169+
stop_time = perf_counter()
170170
duration = (stop_time - start_time) * 1000
171171
_params = ""
172172
try:

debug_toolbar/panels/timer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import time
1+
from time import perf_counter
22

33
from django.template.loader import render_to_string
44
from django.templatetags.static import static
@@ -59,15 +59,15 @@ def scripts(self):
5959
return scripts
6060

6161
def process_request(self, request):
62-
self._start_time = time.time()
62+
self._start_time = perf_counter()
6363
if self.has_content:
6464
self._start_rusage = resource.getrusage(resource.RUSAGE_SELF)
6565
return super().process_request(request)
6666

6767
def generate_stats(self, request, response):
6868
stats = {}
6969
if hasattr(self, "_start_time"):
70-
stats["total_time"] = (time.time() - self._start_time) * 1000
70+
stats["total_time"] = (perf_counter() - self._start_time) * 1000
7171
if hasattr(self, "_start_rusage"):
7272
self._end_rusage = resource.getrusage(resource.RUSAGE_SELF)
7373
stats["utime"] = 1000 * self._elapsed_ru("ru_utime")

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Pending
1212
* Reworked the cache panel instrumentation code to no longer attempt to undo
1313
monkey patching of cache methods, as that turned out to be fragile in the
1414
presence of other code which also monkey patches those methods.
15+
* Update all timing code that used :py:func:`time.time()` to use
16+
:py:func:`time.perf_counter()` instead.
1517

1618
4.0.0 (2023-04-03)
1719
------------------

0 commit comments

Comments
 (0)