Skip to content

Instantly share code, notes, and snippets.

@peterjc
Created March 20, 2012 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjc/2136181 to your computer and use it in GitHub Desktop.
Save peterjc/2136181 to your computer and use it in GitHub Desktop.
Simple "Green Bottles" Python script to benchmark ZLIB calls under different Python implementations + PyPy target
import zlib
import time
def decompress(comp_data):
d = zlib.decompressobj(-15) #Negative window size means no headers
uncomp_data = d.decompress(comp_data) + d.flush()
del d
return uncomp_data, zlib.crc32(uncomp_data)
def compress(orig_data):
c = zlib.compressobj(6,
zlib.DEFLATED,
-15,
zlib.DEF_MEM_LEVEL,
0)
compressed = c.compress(orig_data) + c.flush()
del c
return compressed, zlib.crc32(orig_data)
def check(data):
compressed, original_crc = compress(data)
decompressed, new_crc = decompress(compressed)
if data != decompressed or original_crc != new_crc:
raise ValueError("Data corrupted in ZLIB")
def green_bottles(start=10, plural="green bottles", single="green bottle"):
text = []
count = start
verse = """%(count)i %(plural)s sitting on the wall,
%(count)i %(plural)s sitting on the wall,
And if one %(single)s should accidentally fall,
There'll be %(newcount)i $(plural)s sitting on the wall."""
penultimate = """Two %(plural)s sitting on the wall,
Two %(plural)s sitting on the wall,
And if one %(single)s should accidentally fall,
There'll be one $(single)s sitting on the wall."""
final = """One %(single)s sitting on the wall,
One %(single)s sitting on the wall,
And if one %(single)s should accidentally fall,
There'll be no $(plural)s sitting on the wall."""
while count > 2:
text.append(verse % {"plural":plural,
"single":single,
"count":count,
"newcount":count-1})
count -= 1
if count == 2:
text.append(penultimate % {"plural":plural,
"single":single})
count -= 1
if count == 1:
text.append(final % {"plural":plural,
"single":single})
return "\n\n".join(text)
def entry_point(iterations):
data = green_bottles(1000)
print "Trying ZLIB on %i bytes of text %i times" % (len(data), iterations)
startTime = time.time()
try:
for i in xrange(iterations):
check(data)
result = True
except ValueError:
result = False
endTime = time.time()
return result, startTime, endTime
def main(entry_point = entry_point, iterations = 10000):
print "Green bottles ZLIB benchmark starting... [%r]" % entry_point
result, startTime, endTime = entry_point(iterations)
if not result:
print("Incorrect results!")
return -1
total_s = endTime - startTime
print "Total time for %d iterations: %.2f secs" %(iterations,total_s)
print "Average time per iteration: %.2f ms" %(total_s*1000/iterations)
return 42
if __name__ == '__main__':
import sys
if len(sys.argv) >= 2:
main(iterations = int(sys.argv[1]))
else:
main()
from pypy.translator.goal import green_bottles
entry_point = green_bottles.entry_point
# _____ Define and setup target ___
def target(*args):
return entry_point, [int]
def get_llinterp_args():
return [1]
# _____ Run translated _____
def run(c_entry_point):
print "Translated:"
green_bottles.main(c_entry_point, iterations=10000)
print "CPython:"
green_bottles.main(iterations=10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment