fork download
  1. import copy
  2. import timeit
  3.  
  4. class test1:
  5. def __init__(self, x):
  6. self.x = x
  7.  
  8. def __add__(self, other):
  9. return test1(self.x + other.x)
  10.  
  11. class test2:
  12. def __init__(self, x):
  13. self.x = x
  14.  
  15. def __add__(self, other):
  16. new = copy.deepcopy(self)
  17. new.x = new.x + other.x
  18. return new
  19.  
  20. print(timeit.timeit('x + y', 'x, y = test1(1), test1(2)', globals=globals(), number=10000))
  21. print(timeit.timeit('x + y', 'x, y = test2(1), test2(2)', globals=globals(), number=10000))
Success #stdin #stdout 0.16s 28104KB
stdin
Standard input is empty
stdout
0.006125491112470627
0.14071564748883247