Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Awlex177457y@ThatDude @irene I tried to optimize both codes as hard as possible.
Julia:
function calc_primes()
tic()
max::Int32 = 200000000
primes = ones(Bool, max)
for i::Int32 = 2:max
if primes[i]
j::Int32 = i * 2
while j < max
primes[j] = false
j += i
end
end
end
toc()
# Outside the timemeasure because it's not relevant
primeCount::Int32 = 0
for prime in primes[3:max] # because 0 & 1 are no prims
primeCount += prime
end
println("Amount: $max")
println("Number of primes: $primeCount") # To check whether the results are right
end
calc_primes()
Python:
from time import time
def calc_primes():
max = 200000000
time = time()
primes = [True] * max
for i in range(2, max):
if primes[i]:
j = i * 2
while j < max:
primes[j] = False
j += i
time = time() - time
primeCount = 0
for prime in primes[2:max]:
primeCount += prime
print("Time:", time)
print("Number of primes: ", primeCount)
calc_primes() -
DataSecs9347yhttps://modelingguru.nasa.gov/docs/...
Might be interesting for this discussion. Python almost always is slower than Julia. At some points even very slow. It's "just" Matrix calculation, but anyway is interesting ^^ -
Awlex177457y@DataSec Well, it was kinda expected. Julia was build for high performance and can proudly present itself next to C.
Like I mentioned in a previous comment, the performance was not that off from C, but tbh, I should have closed some running programs for a more accurate comparison -
Huuugo25057yYour python code is weird and far from optimal, mate. You can probably halve that time by optimizing it.
-
DataSecs9347y@Awlex Yea you say it, it's the purpose of it. But measuring the performances of 2 languages is very hard, or generally the performance measurement of a language.
Related Rants
I tried to compare Python and Julia by letting them calculate the first 200000000 prime numbers. The result is dumbfounding.
Python: 95.91282963752747 seconds
Julia: 3.847882271
undefined
primenumbers
python
julia