-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_find_primes.py
63 lines (50 loc) · 1.44 KB
/
example_find_primes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Find primes tests the performance of calling functions that are not been jited (Registered python code) in jited functions
from pyjiting import jit, reg
import time
all_primes = []
all_primes_nojit = []
# jit
@reg
def do_something_jit(x: int) -> int:
print(f'{x} is prime!')
all_primes.append(x)
return 0
@jit
def find_primes_jit(n):
for i in range(2, n):
is_prime = True
for j in range(2, i):
if i % j == 0:
is_prime = False
break
if is_prime == True:
do_something_jit(i)
return 0
# nojit
def do_something_nojit(x: int) -> int:
print(f'{x} is prime!')
all_primes_nojit.append(x)
return 0
def find_primes_nojit(n):
for i in range(2, n):
is_prime = True
for j in range(2, i):
if i % j == 0:
is_prime = False
break
if is_prime == True:
do_something_nojit(i)
return 0
find_primes_nojit(0)
find_primes_jit(0)
start_time = time.time()
result = find_primes_jit(100000)
cost_time_ms = (time.time() - start_time) * 1000
jit_cost = cost_time_ms
start_time = time.time()
result = find_primes_nojit(100000)
cost_time_ms = (time.time() - start_time) * 1000
nojit_cost = cost_time_ms
print('find_primes_jit(100000) =', result, f'(cost time: {jit_cost} ms)')
print('find_primes_nojit(100000) =', result, f'(cost time: {nojit_cost} ms)')
print('rate:', nojit_cost / jit_cost)