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

Performance improvements #11

Merged
merged 3 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[run]
omit =
setup.py
perftest_ulid2.py
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ jobs:
python-version: '${{ matrix.python-version }}'
- run: pip install pytest pytest-cov
- run: py.test -vvv --cov .
- run: python perftest_ulid2.py
- uses: codecov/codecov-action@v2
28 changes: 28 additions & 0 deletions perftest_ulid2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import print_function
import timeit

import ulid2


def perftest():
tmr = timeit.Timer(lambda: ulid2.generate_ulid_as_uuid())
n_iterations = 300000
time_taken = tmr.timeit(n_iterations)
return int(n_iterations / time_taken)


def main():
results = []
for x in range(5):
ops_per_sec = perftest()
print(x + 1, " ... ", ops_per_sec)
results.append(ops_per_sec)
n_results = len(results)
mean = sum(results) / n_results
median = sorted(results)[n_results // 2]
print("mean ops/sec ", mean)
print("median ops/sec", median)


if __name__ == '__main__':
main()
13 changes: 6 additions & 7 deletions ulid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class InvalidULID(ValueError):


def _to_binary(byte_list):
if py3:
return bytes(byte_list)
else:
return bytes(b''.join(chr(b) for b in byte_list))
return bytes(b''.join(chr(b) for b in byte_list))


if py3:
_to_binary = bytes

# Unrolled and optimized ULID Base32 encoding/decoding
# implementations based on NUlid:
# https://github.com/RobThree/NUlid/blob/5f2678b4d/NUlid/Ulid.cs#L159
Expand Down Expand Up @@ -173,6 +173,7 @@ def get_ulid_time(ulid):
_last_entropy = None
_last_timestamp = None


def generate_binary_ulid(timestamp=None, monotonic=False):
"""
Generate the bytes for an ULID.
Expand All @@ -193,9 +194,7 @@ def generate_binary_ulid(timestamp=None, monotonic=False):
timestamp = calendar.timegm(timestamp.utctimetuple())

ts = int(timestamp * 1000.0)
ts_bytes = _to_binary(
(ts >> shift) & 0xFF for shift in (40, 32, 24, 16, 8, 0)
)
ts_bytes = struct.pack('!Q', ts)[2:]
entropy = os.urandom(10)
if monotonic and _last_timestamp == ts and _last_entropy is not None:
while entropy < _last_entropy:
Expand Down