Created
July 11, 2018 15:53
-
-
Save synapticarbors/214fe7c2d8fc3a6deba584311368182c to your computer and use it in GitHub Desktop.
Numba implementation of
This file contains hidden or 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
from __future__ import print_function | |
import numba as nb | |
import random | |
import time | |
from concurrent.futures import ThreadPoolExecutor, as_completed | |
@nb.njit(nogil=True) | |
def flip_coins_single(N): | |
count = 0 | |
for i in range(N): | |
count += random.getrandbits(1) | |
return count | |
def run_parallel(N, ncores): | |
count = 0 | |
with ThreadPoolExecutor(max_workers=ncores) as executor: | |
futures = [executor.submit(flip_coins_single, int(N / ncores)) for _ in range(ncores)] | |
for future in as_completed(futures): | |
count += future.result() | |
return count | |
N = 1e8 | |
t1 = time.time() | |
X = flip_coins_single(N) | |
t2 = time.time() | |
print('JIT warm-up run: {} {}'.format(X, t2 - t1)) | |
t1 = time.time() | |
X = flip_coins_single(N) | |
t2 = time.time() | |
print('Single Core run: {} {}'.format(X, t2 - t1)) | |
for k in [1, 2, 4, 5, 8]: | |
t1 = time.time() | |
X = run_parallel(N, k) | |
t2 = time.time() | |
print('p{} core run: {} {}'.format(k, X, t2 - t1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment