18
Awlex
7y

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

Comments
  • 0
    how the hell?
  • 16
    plot twist: Julia had them all hard coded

    Kappa.
  • 0
    @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()
  • 1
    Btw, a friend of mine did the same in C and his best result was 3535.000000 ms
  • 4
    is Julia cute? 🤔
  • 1
    https://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 ^^
  • 0
    @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
  • 1
    Your python code is weird and far from optimal, mate. You can probably halve that time by optimizing it.
  • 0
    Try comparing Julia with rust ...
  • 0
    @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.
Add Comment