bm_speedup.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2017 gRPC authors.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import math
  17. from scipy import stats
  18. _DEFAULT_THRESHOLD = 1e-10
  19. def scale(a, mul):
  20. return [x * mul for x in a]
  21. def cmp(a, b):
  22. return stats.ttest_ind(a, b)
  23. def speedup(new, old, threshold=_DEFAULT_THRESHOLD):
  24. if (len(set(new))) == 1 and new == old:
  25. return 0
  26. s0, p0 = cmp(new, old)
  27. if math.isnan(p0):
  28. return 0
  29. if s0 == 0:
  30. return 0
  31. if p0 > threshold:
  32. return 0
  33. if s0 < 0:
  34. pct = 1
  35. while pct < 100:
  36. sp, pp = cmp(new, scale(old, 1 - pct / 100.0))
  37. if sp > 0:
  38. break
  39. if pp > threshold:
  40. break
  41. pct += 1
  42. return -(pct - 1)
  43. else:
  44. pct = 1
  45. while pct < 10000:
  46. sp, pp = cmp(new, scale(old, 1 + pct / 100.0))
  47. if sp < 0:
  48. break
  49. if pp > threshold:
  50. break
  51. pct += 1
  52. return pct - 1
  53. if __name__ == "__main__":
  54. new = [0.0, 0.0, 0.0, 0.0]
  55. old = [2.96608e-06, 3.35076e-06, 3.45384e-06, 3.34407e-06]
  56. print(speedup(new, old, 1e-5))
  57. print(speedup(old, new, 1e-5))