fork download
  1. import numpy as np
  2. import time
  3.  
  4. print(np.__version__)
  5.  
  6. loops = 100
  7. size = 80000
  8.  
  9. def pymod(x, y): return x % y
  10.  
  11. def workaround(x, y): return x - (y * np.floor(x / y))
  12.  
  13. #def divmod(x, y): return np.divmod(x, y)[1]
  14.  
  15. a = np.random.rand(size)
  16. a = np.cumsum(a)
  17.  
  18. def test(func, data):
  19. d = 3
  20. start_time = time.time()
  21. for i in range(loops):
  22. b = func(data, d)
  23. d += 0.001
  24. end_time = time.time()
  25.  
  26. print(func.__name__, (end_time - start_time) / loops)
  27.  
  28. test(pymod, a)
  29. test(workaround, a)
  30. test(np.remainder, a)
  31. test(np.mod, a)
  32. test(np.fmod, a)
  33. #test(divmod, a)
  34.  
Success #stdin #stdout 1.29s 92352KB
stdin
Standard input is empty
stdout
1.12.0
pymod 0.0030777502059936524
workaround 0.00045078277587890626
remainder 0.0028792595863342285
remainder 0.0028852272033691407
fmod 0.0023826289176940916