ethereum / eth-utils

Utility functions for working with ethereum related codebases.
https://eth-utils.readthedocs.io/en/latest/
MIT License
320 stars 152 forks source link

Hexadecimal optimizations #223

Closed BoboTiG closed 2 years ago

BoboTiG commented 2 years ago

What was wrong?

Nothing wrong, simply trying to improve performances here, and there.

How was it fixed?

I used that script to naively benchmark changes for the 2nd commit:

import re
from timeit import timeit

def original(value, regex=re.compile("(0x)?[0-9a-f]*", re.IGNORECASE | re.ASCII)):
    return regex.fullmatch(value) is not None

def new(value, regex=re.compile("(0[xX])?[0-9a-fA-F]*")):
    return regex.fullmatch(value) is not None

print("original(True)", timeit(
    "original('0x5831e66b7de5df2d8d4705c86c844ff60a50efcec53f9449b16dc43216a783f7')", "from __main__ import original"))
print("new(True)", timeit(
    "new('0x5831e66b7de5df2d8d4705c86c844ff60a50efcec53f9449b16dc43216a783f7')", "from __main__ import new"))
print()
print("original(False)", timeit(
    "original('0x5831e66b7de5df2d8d4705c86c844ff60a50efcec53f9449b16dc43216a783f7g')", "from __main__ import original"))
print("new(False)", timeit(
    "new('0x5831e66b7de5df2d8d4705c86c844ff60a50efcec53f9449b16dc43216a783f7g')", "from __main__ import new"))

Improvements unlocked by the first commit:

Improvements unlocked by the 2nd commit:

To-Do

Cute Animal Picture

put a cute animal picture link inside the parentheses

BoboTiG commented 2 years ago

I wasn't sure it deserved a news fragment. Here it is!

carver commented 2 years ago

Thanks for the contribution!