-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: speed improvements for IPv4 address validation (#10)
- Loading branch information
1 parent
c79996d
commit e71d857
Showing
3 changed files
with
53 additions
and
22 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import ipaddress | ||
import re | ||
import socket | ||
import timeit | ||
|
||
ipv4 = re.compile(r"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}") | ||
|
||
|
||
def old_validate(ip: str) -> bool: | ||
try: | ||
if ipv4.match(ip): | ||
ipaddress.IPv4Address(ip) | ||
return True | ||
except (ipaddress.AddressValueError, socket.gaierror): | ||
pass | ||
return False | ||
|
||
|
||
def new_validate(ip: str) -> bool: | ||
try: | ||
if ipv4.match(ip): | ||
socket.inet_aton(ip) | ||
return True | ||
except socket.error: | ||
pass | ||
return False | ||
|
||
|
||
def test_code(fun): | ||
assert fun("52.102.136.0") is True | ||
# assert not fun("52.102.136.257") | ||
|
||
|
||
if __name__ == "__main__": | ||
old_speed = timeit.timeit(lambda: test_code(old_validate), number=100000) | ||
print(f"Old speed: {old_speed}") | ||
new_speed = timeit.timeit(lambda: test_code(new_validate), number=100000) | ||
print(f"New speed: {new_speed}") | ||
# More than thrice the speed in positive cases! | ||
# Speed is comparable for negative cases | ||
# It's already a win, pluswe are more likely to get positive cases |
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
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