forked from schedutron/CPAP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete Exercise 4-4-b: multithreaded file reading; used time CLT in…
…stead of timeit
- Loading branch information
1 parent
4158419
commit f36e041
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Script to count occurences of a particular byte in a file. | ||
There was no noticeable difference in single threaded and multithreaded runs. | ||
However, the multithreaded run indeed executed faster (on the order of | ||
milliseconds). Also, more threads didn't necessarily mean more performance, e.g, | ||
a run with 20 threads executed slightly faster than a run with 100 threads. | ||
""" | ||
|
||
import os, sys | ||
from threading import Thread, Lock | ||
|
||
def count(filename, byte, start, end): | ||
global global_count | ||
number = 0 | ||
with open(filename, 'rb') as f: | ||
f.seek(start) | ||
for i in range(start, end): | ||
c = f.read(1) | ||
if c == byte: | ||
number += 1 | ||
lock.acquire() | ||
global_count += number | ||
lock.release() | ||
|
||
lock = Lock() | ||
filename = sys.argv[1] | ||
byte = bytes(sys.argv[2], 'utf-8') | ||
global_count = 0 | ||
filesize = os.stat(filename).st_size | ||
nthreads = int(sys.argv[3]) | ||
|
||
if nthreads > filesize: | ||
print("Too many threads for reading this file! Use less threads.") | ||
quit() | ||
|
||
readsize = filesize // nthreads | ||
ranges = [] | ||
threads = [] | ||
i = 0 | ||
while i < filesize: | ||
ranges.append([i, i+readsize]) | ||
i += readsize | ||
if ranges[-1][1] > filesize: | ||
ranges[-1][1] = filesize | ||
|
||
for pair in ranges: | ||
thread = Thread(target=count, args=(filename, byte, pair[0], pair[1])) | ||
thread.start() | ||
threads.append(thread) | ||
|
||
for thread in threads: | ||
thread.join() | ||
|
||
print(global_count) |
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