FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
fasterPython/fasterPython/fasterPython.py at main · tomtyiu/fasterPython · GitHub
tomtyiu
/
fasterPython
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
13
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
fasterPython
/
fasterPython
/
fasterPython.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (69 loc) · 1.54 KB
Breadcrumbs
fasterPython
/
fasterPython
/
fasterPython.py
Copy path
File metadata and controls
84 lines (69 loc) · 1.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# math fast-path bindings
from
math
import
(
sqrt
,
ceil
,
floor
,
factorial
,
log
,
exp
,
sin
,
pi
,
e
,
tau
,
inf
,
nan
,
)
# ------------------
# Scalar math funcs
# ------------------
def
fast_sqrt
(
x
):
return
sqrt
(
x
)
def
fast_ceil
(
x
):
return
ceil
(
x
)
def
fast_floor
(
x
):
return
floor
(
x
)
def
fast_factorial
(
x
):
return
factorial
(
x
)
def
fast_log
(
x
,
base
=
None
):
return
log
(
x
)
if
base
is
None
else
log
(
x
,
base
)
def
fast_exp
(
x
):
return
exp
(
x
)
def
fast_sin
(
x
):
return
sin
(
x
)
# ------------------
# Math constants
# ------------------
FAST_PI
=
pi
FAST_E
=
e
FAST_TAU
=
tau
FAST_INF
=
inf
FAST_NAN
=
nan
# ------------------
# Cached recursion
# ------------------
from
functools
import
lru_cache
@
lru_cache
(
maxsize
=
None
)
def
fib
(
n
:
int
)
->
int
:
if
n
<
2
:
return
n
return
fib
(
n
-
1
)
+
fib
(
n
-
2
)
# ------------------
# Fast pure-Python sum
# ------------------
def
sum_fast
(
iterable
):
total
=
0
for
v
in
iterable
:
total
+=
v
return
total
# ------------------
# Avoid repeated lookups
# ------------------
def
compute_squares
(
nums
):
result
=
[]
append
=
result
.
append
# local binding
for
n
in
nums
:
append
(
n
*
n
)
return
result
# ------------------
# NumPy fast path (optional)
# ------------------
try
:
import
numpy
as
_np
def
sum_numpy
(
nums
):
return
_np
.
sum
(
_np
.
asarray
(
nums
))
except
ImportError
:
def
sum_numpy
(
nums
):
raise
RuntimeError
(
"NumPy is not installed"
)
Back
|
FazBrowse Home
|
New Git URL