Created
September 6, 2020 15:50
-
-
Save jcrist/1fe77221edddd7342429f5b24a8ec856 to your computer and use it in GitHub Desktop.
Quickle Structs benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [3]: from typing import NamedTuple | |
In [4]: from quickle import Struct | |
In [5]: from dataclasses import dataclass | |
In [6]: class PointTuple(NamedTuple): | |
...: x: int | |
...: y: int | |
...: | |
In [7]: class PointStruct(Struct): | |
...: x: int | |
...: y: int | |
...: | |
In [8]: @dataclass | |
...: class PointDataclass: | |
...: x: int | |
...: y: int | |
...: | |
In [9]: %timeit PointTuple(1, 2) # namedtuples | |
270 ns ± 2.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) | |
In [10]: %timeit PointDataclass(1, 2) # dataclasses | |
262 ns ± 5.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) | |
In [11]: %timeit PointStruct(1, 2) # structs | |
97.9 ns ± 0.723 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) | |
In [12]: import copy | |
In [13]: pt = PointTuple(1, 2) | |
In [14]: pd = PointDataclass(1, 2) | |
In [15]: ps = PointStruct(1, 2) | |
In [16]: %timeit copy.copy(pt) # namedtuples | |
1.87 µs ± 13.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) | |
In [17]: %timeit copy.copy(pd) # dataclasses | |
1.93 µs ± 7.86 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) | |
In [18]: %timeit copy.copy(ps) # structs | |
365 ns ± 2.12 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A quick benchmark of instantiating/copying different struct-like types in python. Quickle structs are faster to instantiate and copy than other equivalent options. Note that all are pretty speedy compared to other python operations, so this likely doesn't matter except for very specific workloads.