Skip to content

Instantly share code, notes, and snippets.

@aliles
aliles / timeit_substring.py
Created July 6, 2011 11:59
Test performance of string slice, buffer() and memoryview() for access a substring.
"Performance test comparing string slice to buffer() and memoryview()."
import os
import timeit
TESTDATA = os.urandom(16 * 1000)
def test_control(s=TESTDATA):
"Use a slice to access a substring of s"
t = s[2:-1]
return len(t)
@aliles
aliles / timeit_tco.py
Created July 7, 2011 12:10
Benchmark a tail call optimisation decorator.
[aliles@macbookpro ~]$ time python2.7 timeit_tco.py 40000
Computed number of length 166714
1.014175 seconds on CPU
real 0m2.109s
user 0m2.094s
sys 0m0.013s
[aliles@macbookpro ~]$ time pypy timeit_tco.py 40000
Computed number of length 166714
@aliles
aliles / timeit_tco.py
Created July 8, 2011 11:30
Benchmark repeated calls to a tail call optimisation decorator.
[aliles@macbookpro ~]$ time python2.7 timeit_tco.py 100 60000
Maximum time on CPU: 0.00030600
Minimum time on CPU: 0.00014800
Average time on CPU: 0.00015401
real 0m9.586s
user 0m9.473s
sys 0m0.110s
[aliles@macbookpro ~]$ time pypy timeit_tco.py 100 60000
@aliles
aliles / hello_world_new.py
Created July 10, 2011 14:10
Syntactic sugar to replace '__name__' idiom with function call
import main
if main.executed():
print 'Hello World'
@aliles
aliles / otd.py
Created July 11, 2011 13:38
One time use dictionary subclass for Python
"One time dictionary class"
from functools import partial, wraps as full_wrap
__all__ = ['OneTimeDict']
wraps = partial(full_wrap, assigned=('__name__', '__doc__'))
class OneTimeDict(dict):
"""One time dictionary object.
@aliles
aliles / decompress_test.py
Created July 12, 2011 12:51
Benchmark decompression performance in Python
"Benchmark decompression performance"
from collections import namedtuple
import argparse
import bz2
import functools
import gzip
import hashlib
import itertools
import logging
import sys
@aliles
aliles / cmdline.py
Created July 13, 2011 13:00
Simple command line parsing using Python's argparse module.
"Simple command line parsing using argparse."
from collections import namedtuple
from functools import partial
import argparse
__all__ = ['Argument', 'parse_cmdline']
class Argument(namedtuple('BaseArgument', 'short full default help')):
"""Command line argument option for command line parser.
@aliles
aliles / hello_world.py
Created July 16, 2011 12:58
Syntactic sugar to replace '__name__' idiom with decorator
from main import execute
@execute
def main():
do_something()
def do_something():
print "Hello World"
@aliles
aliles / chaos.py
Created July 17, 2011 12:19
Logistic map iteration timings
"Logistic map iteration timing"
from itertools import islice
import time
import main
def logistic_map(r, x):
assert r > 0, 'R must be a positive number'
assert 0 < x < 1, 'X must be a number between 0 and 1'
while True:
@aliles
aliles / avro_dur_test.py
Created July 21, 2011 12:55
Rough Avro benchmarking using Geonames dataset
import sys
import time
from avro_geonames import *
if __name__ == '__main__':
iterator = iter_geonames(sys.argv[1])
writer = open_writer()
start = time.clock()
for n in xrange(100000):