Skip to content

Instantly share code, notes, and snippets.

@whoeverest
Created October 11, 2012 13:03
Show Gist options
  • Save whoeverest/3872126 to your computer and use it in GitHub Desktop.
Save whoeverest/3872126 to your computer and use it in GitHub Desktop.
cols and rows
from collections import deque
from random import shuffle, randint
from time import time
def t(msg=None, times=[]):
""" Timing function."""
t = time()
if times and msg:
print msg, t - times[0]
times.append(t)
PATH = 'row.txt'
GEN_ROWS = 1000000
MAX_LEN = 10
COLS = 5
t() # start
with open(PATH, 'w') as f:
f.write('\n'.join(map(str, xrange(GEN_ROWS))))
t('generated rows')
result_columns = [deque(maxlen=MAX_LEN)] * COLS
rows = open(PATH).readlines()
t('loaded rows in memory')
shuffle(rows)
t('shuffled rows')
for row in rows[:MAX_LEN]:
result_columns[randint(0, len(result_columns) - 1)].append(row.strip())
t('end') # end
print map(list, result_columns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment