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