Debian Codegraph @

tools/profiling/microbenchmarks/bm_diff/bm_speedup.py

raw
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
17 import math
18
19 from scipy import stats
20
21 _DEFAULT_THRESHOLD = 1e-10
22
23
24 def scale(a, mul):
25 return [x * mul for x in a]
26
27
28 def cmp(a, b):
29 return stats.ttest_ind(a, b)
30
31
32 def speedup(new, old, threshold=_DEFAULT_THRESHOLD):
33 if (len(set(new))) == 1 and new == old:
34 return 0
35 s0, p0 = cmp(new, old)
36 if math.isnan(p0):
37 return 0
38 if s0 == 0:
39 return 0
40 if p0 > threshold:
41 return 0
42 if s0 < 0:
43 pct = 1
44 while pct < 100:
45 sp, pp = cmp(new, scale(old, 1 - pct / 100.0))
46 if sp > 0:
47 break
48 if pp > threshold:
49 break
50 pct += 1
51 return -(pct - 1)
52 else:
53 pct = 1
54 while pct < 10000:
55 sp, pp = cmp(new, scale(old, 1 + pct / 100.0))
56 if sp < 0:
57 break
58 if pp > threshold:
59 break
60 pct += 1
61 return pct - 1
62
63
64 if __name__ == "__main__":
65 new = [0.0, 0.0, 0.0, 0.0]
66 old = [2.96608e-06, 3.35076e-06, 3.45384e-06, 3.34407e-06]
67 print(speedup(new, old, 1e-5))
68 print(speedup(old, new, 1e-5))