Created
July 13, 2022 19:47
-
-
Save luhn/6e530596a4e3727ff936d481e7a24b87 to your computer and use it in GitHub Desktop.
ABC Benchmark
This file contains hidden or 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
import timeit | |
from abc import ABC, abstractmethod | |
class A(ABC): | |
@abstractmethod | |
def a(self): | |
... | |
class B(A): | |
def a(self): | |
return 1 | |
class C: | |
def a(self): | |
return 1 | |
def test_abc(): | |
b = B() | |
b.a() | |
def test_no_abc(): | |
c = C() | |
c.a() | |
NUM_RUNS = 10 ** 7 | |
abc_time = timeit.Timer(test_abc).timeit(number=NUM_RUNS) | |
no_abc_time = timeit.Timer(test_no_abc).timeit(number=NUM_RUNS) | |
print(f'w/ ABC: {abc_time:.2}') | |
print(f'w/o ABC: {no_abc_time:.2}') |
This file contains hidden or 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
w/ ABC: 1.4 | |
w/o ABC: 1.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment