Last active
August 17, 2020 14:19
-
-
Save dunossauro/f27452ec2ad88270224cfa48ef69c206 to your computer and use it in GitHub Desktop.
cc
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
""" | |
$ pip install numba | |
$ python jit.py | |
resultados: | |
$ python test_cc.py | |
Tempos com JIT | |
cc_0_jit - Min: 0.15884569298941642, Max: 0.1812134649953805, Mean: 0.16276181136781814 | |
cc_1_jit - Min: 0.15603229100815952, Max: 0.18515240401029587, Mean: 0.16477074182126672 | |
cc_2_jit - Min: 0.1618276800145395, Max: 0.25311970300390385, Mean: 0.17394073442847002 | |
cc_3_jit - Min: 0.1659998889954295, Max: 0.2548448049928993, Mean: 0.17590742760221473 | |
cc_3_loop_jit - Min: 0.1471281429985538, Max: 0.26632445299765095, Mean: 0.16636071566026658 | |
Tempos sem JIT | |
cc_0 - Min: 0.06313921601395123, Max: 0.09617432300001383, Mean: 0.06712803481175797 | |
cc_1 - Min: 0.07579782500397414, Max: 0.0936408819979988, Mean: 0.07817688702954911 | |
cc_2 - Min: 0.08451158300158568, Max: 0.09043807699345052, Mean: 0.08629662264720536 | |
cc_3 - Min: 0.0900561090093106, Max: 0.14318020700011402, Mean: 0.09299033465969841 | |
cc_3_loop - Min: 58.21612359400024, Max: 64.1921016240085, Mean: 60.712612782407085 | |
""" | |
from statistics import mean | |
from timeit import repeat | |
from numba import njit | |
def means(stmt, r=100): | |
result = repeat(stmt, globals=globals(), repeat=r) | |
return f'- Min: {min(result)}, Max: {max(result)}, Mean: {mean(result)}' | |
def cc_0(x): | |
return x | |
def cc_1(x): | |
if x: | |
return x | |
def cc_2(x): | |
if x: | |
if x: | |
return x | |
def cc_3(x): | |
if x: | |
if x: | |
if x: | |
return x | |
def cc_3_loop(x): | |
for x in range(x): | |
if x ** 2: | |
if x ** 3: | |
x + 3 | |
cc_0_jit = njit(cc_0) | |
cc_1_jit = njit(cc_1) | |
cc_2_jit = njit(cc_2) | |
cc_3_jit = njit(cc_3) | |
cc_3_loop_jit = njit(cc_3_loop) | |
cc_0_jit(1000000000) # primeira execução para compilação | |
cc_1_jit(1000000000) # primeira execução para compilação | |
cc_2_jit(1000000000) # primeira execução para compilação | |
cc_3_loop_jit(1000000000) # primeira execução para compilação | |
print('Tempos com JIT') | |
print(f"cc_0_jit {means('cc_0_jit(1000000)')}") | |
print(f"cc_1_jit {means('cc_1_jit(1000000)')}") | |
print(f"cc_2_jit {means('cc_2_jit(1000000)')}") | |
print(f"cc_3_jit {means('cc_3_jit(1000000)')}") | |
print(f"cc_3_loop_jit {means('cc_3_loop_jit(1000000)')}") | |
print('Tempos sem JIT') | |
print(f"cc_0 {means('cc_0(1000000)')}") | |
print(f"cc_1 {means('cc_1(1000000)')}") | |
print(f"cc_2 {means('cc_2(1000000)')}") | |
print(f"cc_3 {means('cc_3(1000000)')}") | |
print(f"cc_3_loop {means('cc_3_loop(100)', r=5)}") |
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
$ python -mtimeit -s'import test' 'test.cc_0(10000000)' | |
5000000 loops, best of 5: 78.2 nsec per loop | |
$ python -mtimeit -s'import test' 'test.cc_1(10000000)' | |
5000000 loops, best of 5: 86.4 nsec per loop | |
$ python -mtimeit -s'import test' 'test.cc_2(10000000)' | |
5000000 loops, best of 5: 90.9 nsec per loop | |
$ python -mtimeit -s'import test' 'test.cc_3(10000000)' | |
5000000 loops, best of 5: 95.9 nsec per loop |
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
def cc_0(x): | |
return x | |
def cc_1(x): | |
if x: | |
return x | |
def cc_2(x): | |
if x: | |
if x: | |
return x | |
def cc_3(x): | |
if x: | |
if x: | |
if x: | |
return x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment