Created
November 28, 2015 16:32
-
-
Save alq666/fee4d754305add822bf7 to your computer and use it in GitHub Desktop.
Micro-benchmarks for gauge deltas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
import random | |
acc = None | |
def sample(): | |
value = random.random() | |
if value >= 0.5: | |
return "+" + str(value) | |
else: | |
return str(value) | |
# | |
def gauge_delta(): | |
global acc | |
value = sample() | |
try: | |
if len(str(value)) > 0 and str(value)[0] in ('+'): | |
if acc is None: | |
acc = 0 | |
acc += float(value) | |
else: | |
acc = float(value) | |
except ValueError: | |
raise Exception("Cannot parse gauge value") | |
def gauge(): | |
value = sample() | |
try: | |
acc = int(value) | |
except ValueError: | |
try: | |
acc = float(value) | |
except ValueError: | |
raise Exception("Metric value must be a number") | |
def gauge_fast(): | |
value = sample() | |
try: | |
acc = float(value) | |
except ValueError: | |
raise Exception("Metric value must be a number") | |
def main(): | |
print "--- Gauge only ---" | |
print timeit.repeat("gauge()", "from __main__ import gauge") | |
print "--- Gauge delta ---" | |
print timeit.repeat("gauge_delta()", "from __main__ import gauge_delta, acc") | |
print "--- Gauge fast ---" | |
print timeit.repeat("gauge_fast()", "from __main__ import gauge_fast") | |
if __name__ == '__main__': | |
main() |
Author
alq666
commented
Nov 28, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment