Skip to content

Instantly share code, notes, and snippets.

@squeaky-pl
Created March 19, 2014 15:39
Show Gist options
  • Save squeaky-pl/9644389 to your computer and use it in GitHub Desktop.
Save squeaky-pl/9644389 to your computer and use it in GitHub Desktop.
from __future__ import division
from threading import Thread
from array import array
from sys import argv
data = array('B')
with open('/dev/urandom') as f:
data.fromfile(f, 1 << 26)
def add(start, stop):
acc = 0
for i in range(start, stop):
acc += data[i]
return acc
if __name__ == '__main__':
opt = int(argv[1])
if opt == 0:
print add(0, len(data))
else:
import time
start = time.time()
chunk = len(data) / opt
threads = [Thread(target=add, args=(int(i * chunk), int((i + 1) * chunk))) for i in range(opt)]
for t in threads:
t.start()
for t in threads:
t.join()
print time.time() - start
(tmp)squeaky@tannit:~$ /home/arigo/br/stmgc-c7/64compiled/pypy-c-r70087-stm test.py 1
18.977668047
(tmp)squeaky@tannit:~$ /home/arigo/br/stmgc-c7/64compiled/pypy-c-r70087-stm test.py 2
18.948595047
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment