forked from numba/numba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry.py
47 lines (40 loc) · 1.26 KB
/
try.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
import time
import numpy as np
from numba import jit
def timer(func):
def timer_decorator(*args, **kargs):
tStart = time.perf_counter()
# print(args)
func(*args, **kargs)
print(f"Exec time of {func.__class__.__name__} is {time.perf_counter() - tStart}s")
return timer_decorator
@timer
def pythagorean_triples(max_n=100):
"""Find Pythagorean triples with a <= max_n."""
triples = []
for a in range(1, max_n + 1):
for b in range(a, max_n + 1):
for c in range(b, max_n + 1):
if a ** 2 + b ** 2 == c ** 2:
triples.append((a, b, c))
return triples
@timer
@jit
def numba_pythagorean_triples(max_n=100):
"""Find Pythagorean triples with a <= max_n."""
triples = []
for a in range(1, max_n + 1):
for b in range(a, max_n + 1):
for c in range(b, max_n + 1):
if a ** 2 + b ** 2 == c ** 2:
triples.append((a, b, c))
return triples
numba_pythagorean_triples(1000)
# tStart = time.perf_counter()
# numba_pythagorean_triples(1000)
# print(f"Exec time of {numba_pythagorean_triples.__class__.__name__} is {time.perf_counter() - tStart}s")
# pythagorean_triples(100)
# @jit
# def add(a, b):
# return a + b
# add(1, 3)