Dudes,recently I reviewed Python and the Taichi package. As lots of users mentioned, the performance enchanced almost 100x by Taichi. And, when I compare with the counting primes code between Julia and Python, it seems they are almost the same, maybe there is something wrong in my code.
Here is the code in Python:
"""Count the number of primes in range [1, n].
"""
def is_prime(n: int):
result = True
for k in range(2, int(n ** 0.5) + 1):
if n % k == 0:
result = False
break
return result
def count_primes(n: int) -> int:
count = 0
for k in range(2, n):
if is_prime(k):
count += 1
return count
print(count_primes(1000000))
time python count_primes.py
real 0m2.235s
user 0m2.235s
sys 0m0.000s
and here is the code in Julia:
function is_prime(n::Int)
result=true
for k in (2:(trunc(n^0.5)+1))
if n % k ==0
result=false
break
end
end
return result
end
function count_primes(n::Int)
count = 0
for k in (2:n)
if is_prime(k)
count+=1
end
end
return count
end
@timev println(count_primes(1000000))
2.659729 seconds (10 allocations: 272 bytes)
elapsed time (ns): 2659728961
bytes allocated: 272
pool allocs: 10
and here is the code in Python with Taichi:
"""Count the number of primes below a given bound.
"""
import taichi as ti
ti.init()
@ti.func
def is_prime(n: int):
result = True
for k in range(2, int(n ** 0.5) + 1):
if n % k == 0:
result = False
break
return result
@ti.kernel
def count_primes(n: int) -> int:
count = 0
for k in range(2, n):
if is_prime(k):
count += 1
return count
print(count_primes(1000000))
time python count_primes.py
real 0m0.363s
user 0m0.546s
sys 0m0.179s
I know Julia is fast, but in this condition by far in this condition, it seems Julia is the same with Python and the Taichi is a booster. If someone hotshot can do me a favor on this question please?
and there is a framework of Taichi I find:
I am all gratitude!