Skip to content

Optimize JSON encoding of numbers - #458

Merged
jcrist merged 1 commit into
mainfrom
optimize-json-number-encoding
Jun 28, 2023
Merged

jcrist merged 1 commit into
mainfrom
optimize-json-number-encoding

Conversation

@jcrist

@jcrist jcrist commented Jun 28, 2023

Copy link
Copy Markdown
Member

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
@jcrist
jcrist deleted the optimize-json-number-encoding branch June 28, 2023 02:47
@jcrist

jcrist commented Jun 28, 2023

Copy link
Copy Markdown
Member Author

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