aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@google.com>2018-03-22 16:51:24 +0100
committerGravatar Jan Tattermusch <jtattermusch@google.com>2018-03-22 17:35:28 +0100
commit9f40b5f29361d74c4891d8dfee112cff6a2d13c0 (patch)
tree9fc352f45cfedc61c14f0a4a5be6b4387b35dca9
parent577e6379200f629c0bab3c954221dd22b9520f41 (diff)
further improve percentile() code
-rwxr-xr-xtools/profiling/latency_profile/profile_analyzer.py15
1 files changed, 7 insertions, 8 deletions
diff --git a/tools/profiling/latency_profile/profile_analyzer.py b/tools/profiling/latency_profile/profile_analyzer.py
index ad453a9eb0..cdc2f1cbab 100755
--- a/tools/profiling/latency_profile/profile_analyzer.py
+++ b/tools/profiling/latency_profile/profile_analyzer.py
@@ -194,14 +194,13 @@ def percentile(N, percent, key=lambda x: x):
"""
if not N:
return None
- idx = (len(N) - 1) * percent
- idx_floor = math.floor(idx)
- idx_ceil = math.ceil(idx)
- if idx_floor != idx_ceil:
- # interpolate the nearest element values
- return (key(N[int(idx_floor)]) * (idx_ceil - idx) +
- key(N[int(idx_ceil)]) * (idx - idx_floor))
- return key(N[int(idx)])
+ float_idx = (len(N) - 1) * percent
+ idx = int(float_idx)
+ result = key(N[idx])
+ if idx < len(N) - 1:
+ # interpolate with the next element's value
+ result += (float_idx - idx) * (key(N[idx + 1]) - key(N[idx]))
+ return result
def tidy_tag(tag):