Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use time.perf_counter() instead of time.time() to measure performance #1157

Closed
simonw opened this issue Dec 21, 2020 · 1 comment
Closed

Comments

@simonw
Copy link
Owner

simonw commented Dec 21, 2020

I do that in a bunch of places: https://ripgrep.datasette.io/-/ripgrep?pattern=time%28%29&literal=on&glob=datasette%2F%2A%2A

https://docs.python.org/3/library/time.html#time.perf_counter

time.``perf_counter() → float

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

New in version 3.3.

@simonw
Copy link
Owner Author

simonw commented Dec 21, 2020

Three places to fix:

start = time.time()
yield
end = time.time()

@contextmanager
def sqlite_timelimit(conn, ms):
deadline = time.time() + (ms / 1000)
# n is the number of SQLite virtual machine instructions that will be
# executed between each check. It's hard to know what to pick here.
# After some experimentation, I've decided to go with 1000 by default and
# 1 for time limits that are less than 50ms
n = 1000
if ms < 50:
n = 1
def handler():
if time.time() >= deadline:
return 1

end = time.time()
data["query_ms"] = (end - start) * 1000

@simonw simonw closed this as completed in 810853c Dec 21, 2020
simonw added a commit that referenced this issue Jan 19, 2021
@simonw simonw added this to the Datasette 0.54 milestone Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant