Last active
August 29, 2015 14:17
-
-
Save suvirbhargav/85fe8a45a87f15218ef3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
so, i saw c++11 example on nvidia blog about using threads for doing things in parallel. | |
don't know about the speed, but the code isn't that intuitive, so i wrote one python as multiprocessing feels easier and cleaner in python. | |
''' | |
import multiprocessing | |
import time | |
def process_article(single_character): | |
f = open('txt_files/warandpeace.txt', 'r') | |
return sum(line.count(single_character) for line in f) | |
# let's calculate time | |
now = time.time() | |
processes=multiprocessing.cpu_count() | |
pool = multiprocessing.Pool(processes) | |
#for group in char_range('w', 'z'): | |
#it = pool.imap(process_article, char_range('w', 'z')) | |
it = pool.imap(process_article, map(chr, range(119, 123))) | |
pool.close() #we are not adding any more processes | |
pool.join() #tell it to wait until all threads are done before going on | |
print sum(it) | |
# print total time taken | |
print "Finished in", time.time()-now , "sec" | |
''' | |
In [2]: execfile('gist1.py') | |
107310 | |
Finished in 0.11798119545 sec | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment