Skip to content

Instantly share code, notes, and snippets.

@fffergal
Created March 11, 2018 00:58
Show Gist options
  • Save fffergal/cbdf43bb29d52be46da4a34ddbf0f0c7 to your computer and use it in GitHub Desktop.
Save fffergal/cbdf43bb29d52be46da4a34ddbf0f0c7 to your computer and use it in GitHub Desktop.
# With 100 classes
In [34]: things = {i: list(type('hey', (object,), {})() for j in range(10)) for i in range(10
...: )}
In [35]: %%timeit -o the_type = type(choice(list(instance for key, instances in things.items(
...: ) for instance in instances)))
...: any(the_type is type(instance) for key, instances in things.items() for instance in
...: instances)
The slowest run took 16.05 times longer than the fastest. This could mean that an intermediate result is being cached.
9.32 µs ± 5.88 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Out[35]: <TimeitResult : 9.32 µs ± 5.88 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)>
In [36]: _.best, _.worst
Out[36]: (1.1125606996938587e-06, 1.7851611259393395e-05)
In [37]: %%timeit -o the_type = type(choice(list(instance for key, instances in things.items(
...: ) for instance in instances)))
...: types = set(type(instance) for key, instances in things.items() for instance in inst
...: ances)
...: the_type in types
22.1 µs ± 2.49 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Out[37]: <TimeitResult : 22.1 µs ± 2.49 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)>
In [38]: _.best, _.worst
Out[38]: (2.091267928481102e-05, 2.8161621699109674e-05)
# With 1000 classes
In [39]: things = {i: list(type('hey', (object,), {})() for j in range(10)) for i in range(10
...: 0)}
In [40]: %%timeit -o the_type = type(choice(list(instance for key, instances in things.items(
...: ) for instance in instances)))
...: any(the_type is type(instance) for key, instances in things.items() for instance in
...: instances)
The slowest run took 4.71 times longer than the fastest. This could mean that an intermediate
result is being cached.
113 µs ± 50 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Out[40]: <TimeitResult : 113 µs ± 50 µs per loop (mean ± std. dev. of 7 runs, 10000 loops eac
h)>
In [41]: _.best, _.worst
Out[41]: (4.190067239105701e-05, 0.0001972935355268419)
In [42]: %%timeit -o the_type = type(choice(list(instance for key, instances in things.items(
...: ) for instance in instances)))
...: types = set(type(instance) for key, instances in things.items() for instance in inst
...: ances)
...: the_type in types
180 µs ± 6.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Out[42]: <TimeitResult : 180 µs ± 6.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops eac
h)>
In [43]: _.best, _.worst
Out[43]: (0.00017562702903524042, 0.00019580388022586703)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment