-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Benchmark.py
100 lines (79 loc) · 2.61 KB
/
Benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import timeit
loops = 1_000_000
val = timeit.timeit("""Luhn("9999999999 9999999999 9999999999 9999999999").valid()""",
"""
class Luhn:
def __init__(self, card_num):
self.isValid = Luhn.luhny_bin(card_num)
def valid(self):
return self.isValid
@staticmethod
def luhny_tune(num):
return dbl - 9 if (dbl := 2 * num) > 9 else dbl
@staticmethod
def luhny_bin(num):
total = 0
pos = 0
for ltr in reversed(num):
if ltr.isdigit():
if not pos % 2:
total+= int(ltr)
else:
total += Luhn.luhny_tune(int(ltr))
pos += 1
elif ltr != " ":
return False
return pos > 1 and not total % 10
""", number=loops) / loops
print(f"reversed for: {val}")
val = timeit.timeit("""Luhn("9999999999 9999999999 9999999999 9999999999").valid()""",
"""
class Luhn:
def __init__(self, card_num):
self.isValid = Luhn.luhny_bin(card_num)
def valid(self):
return self.isValid
@staticmethod
def luhny_tune(num):
return dbl - 9 if (dbl := 2 * num) > 9 else dbl
@staticmethod
def luhny_bin(num):
num = num.replace(' ', '')
if not num.isdigit():
return False
total = 0
for pos, ltr in enumerate(num[::-1]):
if not pos % 2:
total+= int(ltr)
else:
total += Luhn.luhny_tune(int(ltr))
pos += 1
return pos > 1 and not total % 10
""", number=loops) / loops
print(f"replace reverse enumerate: {val}")
val = timeit.timeit("""Luhn("9999999999 9999999999 9999999999 9999999999").valid()""",
"""
class Luhn:
def __init__(self, card_num):
self.isValid = Luhn.luhny_bin(0, 0, list(card_num[::-1]))
def valid(self):
return self.isValid
@staticmethod
def luhny_tune(num):
return dbl - 9 if (dbl := 2 * num) > 9 else dbl
@staticmethod
def luhny_bin(pos, sum, chars):
if not chars:
return pos > 1 and sum % 10 == 0
else:
head, *tail = chars
if head.isdigit():
if not pos % 2:
return Luhn.luhny_bin(pos + 1, sum + int(head), tail)
else:
return Luhn.luhny_bin(pos + 1, sum + Luhn.luhny_tune(int(head)), tail)
if head == " ":
return Luhn.luhny_bin(pos, sum, tail)
return False
""", number=loops) / loops
print(f"recursion: {val}")