Skip to content

Instantly share code, notes, and snippets.

@luhn
Created July 13, 2022 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luhn/6e530596a4e3727ff936d481e7a24b87 to your computer and use it in GitHub Desktop.
Save luhn/6e530596a4e3727ff936d481e7a24b87 to your computer and use it in GitHub Desktop.
ABC Benchmark
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}')
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