Skip to content

Instantly share code, notes, and snippets.

@SpotlightKid
Forked from mrocklin/benchmark.py
Last active January 3, 2016 05:49
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 SpotlightKid/8418425 to your computer and use it in GitHub Desktop.
Save SpotlightKid/8418425 to your computer and use it in GitHub Desktop.
from contextlib import contextmanager
from operator import itemgetter
from sys import argv, stdout
from time import time
@contextmanager
def duration(outfile=stdout):
start = time()
yield
end = time()
outfile.write(str(end - start) + '\n')
def groupby(seq, func):
d = dict()
for item in seq:
d.setdefault(func(item), []).append(item)
return d
with duration():
with open(argv[1], 'r') as file:
word_pairs = [line.strip().split(',') for line in file]
result = groupby(word_pairs, itemgetter(0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment