Skip to content

Commit

Permalink
feat(Python): add Numba
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Jun 30, 2024
1 parent edadcce commit b59e1bd
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ RUN apt install -y \
pipx \
python3-venv \
python3-ipython
RUN pipx install numba && pipx install cython
ENV PATH="/root/.local/bin:${PATH}"

# PyPy
ARG PYPY=v7.3.15
Expand Down
1 change: 1 addition & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.so
17 changes: 16 additions & 1 deletion python/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,30 @@ strategy:
command: 'pypy3 %s.py'
tags:
- Alternative
- title: 'Numba'
command: 'numba %s-Numba.py'
tags:
- Alternative
- JIT

exclude:
- command: 'Numba'
file: linpack/Linpack

- command: 'Numba'
file: primes/Atkin

- command: 'Numba'
file: treap/Naive

files:
- linpack/Linpack

- primes/Simple
- primes/Atkin

- treap/Naive

- recursion/Tak
- primes/Simple
- collatz/MaxSequence
- mandelbrot/Simple
35 changes: 35 additions & 0 deletions python/collatz/MaxSequence-Numba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import time
from numba import jit

NUMBER = 500000

@jit(nopython=True)
def collatz(x):
len = 0
while x > 1:
if x % 2 == 0:
x = x // 2
else:
x = 3 * x + 1
len += 1
return len

@jit(nopython=True)
def findMaxCollatz(to):
result = (1, 1)
for number in range(1, to + 1):
len = collatz(number)
if len > result[1]:
result = (number, len)
return result

if __name__ == "__main__":
startTimeMs = int(round(time.time() * 1000))

result = findMaxCollatz(NUMBER)
print(result)

endTimeMs = int(round(time.time() * 1000))
executionTime = endTimeMs - startTimeMs

print(f"Execution time: {executionTime}ms")
49 changes: 49 additions & 0 deletions python/mandelbrot/Simple-Numba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import time
from numba import jit

def index():
for y in range(-39, 39):
print()
for x in range(-39, 39):
i = mandelbrot(x / 40.0, y / 40.0)

if i == 0:
print("*", end="")
else:
print(" ", end="")


@jit(nopython=True)
def mandelbrot(x, y):
cr = y - 0.5
ci = x
zi = 0.0
zr = 0.0
i = 0

while True:
i += 1

temp = zr * zi

zr2 = zr ** 2
zi2 = zi ** 2

zr = zr2 - zi2 + cr
zi = 2 * temp + ci

if zi2 + zr2 > 16:
return i

if i > 5000:
return 0

if __name__ == "__main__":
startTimeMs = int(round(time.time() * 1000))

index()

endTimeMs = int(round(time.time() * 1000))
executionTime = endTimeMs - startTimeMs

print(f"Execution time: {executionTime}ms")
34 changes: 34 additions & 0 deletions python/primes/Simple-Numba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import time
from math import sqrt
import numba

limit = 500000

@numba.jit(nopython=True)
def get_last_prime(count):
last_prime = 2

# Traverse each number from 1 to N with the help of for loop
for num in range(3, count, 2):
is_prime = True
limit = int(sqrt(num))

for i in range(3, limit, 2):
if num % i == 0:
is_prime = False
break

if is_prime:
last_prime = num

return last_prime

if __name__ == "__main__":
start_time_ms = int(round(time.time() * 1000))

last_prime = get_last_prime(limit)
print(f"Last prime: {last_prime}")

end_time_ms = int(round(time.time() * 1000))
execution_time = end_time_ms - start_time_ms
print(f"Execution time: {execution_time}ms")
17 changes: 17 additions & 0 deletions python/recursion/Tak-Numba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import time
from numba import jit

@jit(nopython=True)
def tak(x, y, z):
if (y < x):
return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y))
else:
return z

startTimeMs = int(round(time.time() * 1000))

print(tak(30, 22, 12))

endTimeMs = int(round(time.time() * 1000))
executionTime = endTimeMs - startTimeMs
print(f"Execution time: {executionTime}ms")

0 comments on commit b59e1bd

Please sign in to comment.