ethereum / hexbytes

Python `bytes` subclass that decodes hex, with a readable console output
MIT License
27 stars 19 forks source link

Improve performances #19

Closed BoboTiG closed 2 years ago

BoboTiG commented 2 years ago

Quick benchmarks (first line is the original code, second is the patched one):

$ python -m timeit -n 1000000 -s "from hexbytes import HexBytes" "HexBytes(0)"
1000000 loops, best of 5: 1.260 usec per loop
1000000 loops, best of 5: 0.875 nsec per loop (-30.5 %)

$ python -m timeit -n 1000000 -s "from hexbytes import HexBytes" "HexBytes('0')"
1000000 loops, best of 5: 883 nsec per loop
1000000 loops, best of 5: 760 nsec per loop (-13.9 %)

$ python -m timeit -n 1000000 -s "from hexbytes import HexBytes" "HexBytes(False)"
1000000 loops, best of 5: 540 nsec per loop
1000000 loops, best of 5: 447 nsec per loop (-17.2 %)

What was wrong?

Nothing.

How was it fixed?

Gut feeling.

I had to bypass mypy errors as they are not revelant.

To-Do

Cute Animal Picture

put a cute animal picture link inside the parentheses

carver commented 2 years ago
$ python -m timeit -n 1000000 -s "from hexbytes import HexBytes" "HexBytes(0)"
1000000 loops, best of 5: 1.260 usec per loop
1000000 loops, best of 5: 0.875 nsec per loop (-30.5 %)

I assume that usec is wrong, or it got >1000x faster? 😆

BoboTiG commented 2 years ago
$ python -m timeit -n 1000000 -s "from hexbytes import HexBytes" "HexBytes(0)"
1000000 loops, best of 5: 1.260 usec per loop
1000000 loops, best of 5: 0.875 nsec per loop (-30.5 %)

I assume that usec is wrong, or it got >1000x faster? laughing

I tried again, and indeed, I did a "small" mistake:

# From the master branch
$ python -m timeit -n 1000000 -s "from hexbytes import HexBytes" "HexBytes(0)"
1000000 loops, best of 5: 1.2 usec per loop

# From the branch of that PR
$ python -m timeit -n 1000000 -s "from hexbytes import HexBytes" "HexBytes(0)"
1000000 loops, best of 5: 906 nsec per loop