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

Optimize JSON encoding of numbers #458

Merged
merged 1 commit into from
Jun 28, 2023
Merged

Conversation

jcrist
Copy link
Owner

@jcrist jcrist commented Jun 28, 2023

This further optimizes the JSON encoding of integers and floats.

This further optimizes the JSON encoding of integers and floats.
@jcrist jcrist merged commit a9457e1 into main Jun 28, 2023
7 checks passed
@jcrist jcrist deleted the optimize-json-number-encoding branch June 28, 2023 02:47
@jcrist
Copy link
Owner Author

jcrist commented Jun 28, 2023

A quick benchmark:

import random
from time import perf_counter_ns

from orjson import dumps
from msgspec.json import encode

N = 10000
M = 2000
random.seed(42)
for header, scale, func in [
    ("Small Integers:", 1000, random.randint),
    ("Large Integers:", int(1e16), random.randint),
    ("Small Floats:", 1000, random.uniform),
    ("Large Floats:", int(1e16), random.uniform),
]:
    print(header)
    data = [func(-scale, scale) for _ in range(N)]

    start = perf_counter_ns()
    for _ in range(M):
        encode(data)
    stop = perf_counter_ns()
    print(f"- msgspec: {(stop - start) / (1000 * M):.2f} us")

    start = perf_counter_ns()
    for _ in range(M):
        dumps(data)
    stop = perf_counter_ns()
    print(f"- orjson: {(stop - start) / (1000 * M):.2f} us")

Before

$ python bench.py
Small Integers:
- msgspec: 122.89 us
- orjson: 137.58 us
Large Integers:
- msgspec: 228.17 us
- orjson: 376.78 us
Small Floats:
- msgspec: 374.01 us
- orjson: 355.98 us
Large Floats:
- msgspec: 404.31 us
- orjson: 395.41 us

This PR

$ python bench.py
Small Integers:
- msgspec: 132.78 us
- orjson: 143.62 us
Large Integers:
- msgspec: 189.28 us
- orjson: 377.78 us
Small Floats:
- msgspec: 351.13 us
- orjson: 356.19 us
Large Floats:
- msgspec: 391.72 us
- orjson: 395.72 us

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant