Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python vs Julia speed comparison

Tags:

python

julia

I tried to compare these two snippets and see how many iterations could be done in one second. Turns out that Julia achieves 2.5 million iterations whereas Python 4 million. Isn't Julia supposed to be quicker. Or maybe these two snippets are not equivalent?

Python:

t1 = time.time()
i = 0
while True:
    i += 1
    if time.time() - t1 >= 1:
        break

Julia:

function f()
    i = 0
    t1 = now()
    while true
        i += 1
        if now() - t1 >= Base.Dates.Millisecond(1000)
            break
        end
    end
    return i
end
like image 288
Michas Avatar asked Apr 23 '20 12:04

Michas


People also ask

Is Julia slower than Python?

Julia seems an order of magnitude slower than Python when printing to the terminal, because of issue with "sleep" - General Usage - Julia Programming Language.

Is Julia the fastest language?

Julia is fast. In fact, optimized Julia code can be just as fast as highly optimized C++ or Fortran code. Moreove, this code-optimization process will be much easier to accomplish in Julia, and the resulting program will require a factor of 2X or 3X fewer lines of code.

Is Julia faster than NumPy?

Array-wise expression (with temporaries) More interesting is the scaling with array size. For small arrays (up to 1000 elements) Julia is actually faster than Python/NumPy. For intermediate size arrays (100,000 elements), Julia is nearly 2.5 times slower (and in fact, without the sum , Julia is up to 4 times slower).

How much faster is Python than Ruby?

In conclusion, Python is notably faster than Ruby when it comes to raw speed with around 50% margin victory in the 'for' loop test whereas Ruby is a clear winner against Python when it comes to recursion test.


2 Answers

This is kind of an odd performance comparison since typically one measures the time it takes to compute something of substance, rather than seeing how many trivial iterations one can do in a certain amount of time. I had trouble getting your Python and Julia codes to work, so I modified the Julia code to work and just didn't run the Python code. As noted by @chepner in a comment, using now() and doing time comparisons with DateTime objects is fairly expensive. The Python time.time() function just returns a floating-point value. As it turns out, there's a Julia function called time() that does the exact same thing:

julia> time()
1.587648091474481e9

Here's the timing of your original f() function (modified to work) on my system:

julia> using Dates

julia> function f()
           i = 0
           t1 = now()
           while true
               i += 1
               if now() - t1 >= Millisecond(1000)
                   break
               end
           end
           return i
       end
f (generic function with 1 method)

julia> f()
4943739

It did almost 5 million iterations before time was up. As I said, I wasn't able to get your Python code to run on my system without significant fiddling (which I didn't bother doing). But here's a version of f() that uses time() instead, which I will imaginatively call g():

julia> function g()
           i = 0
           t1 = time()
           while true
               i += 1
               if time() - t1 >= 1
                   break
               end
           end
           return i
       end
g (generic function with 1 method)

julia> g()
36087637

This version did 36 million iterations. So I guess Julia is faster at looping? Yay! Well, actually the main work in this loop is the calls to time() so... Julia is faster at generating lots of time() calls!

Why is it odd to time this? As I said, most of actual work here is calling time(). The rest of the loop doesn't really do anything. In an optimizing compiled language, if the compiler sees a loop that doesn't do anything, it will eliminate it entirely. For example:

julia> function h()
           t = 0
           for i = 1:100_000_000
               t += i
           end
           return t
       end
h (generic function with 1 method)

julia> h()
5000000050000000

julia> @time h()
  0.000000 seconds
5000000050000000    

Woah, zero seconds! How is that possible? Well, let's look at the LLVM code (kind of like machine code but for an imaginary machine that is used as an intermediate representation) this lowers to:

julia> @code_llvm h()

;  @ REPL[16]:1 within `h'
define i64 @julia_h_293() {
top:
;  @ REPL[16]:6 within `h'
  ret i64 5000000050000000
}

The compiler sees the loop, figures out that the result is the same every time, and just returns that constant value instead of actually executing the loop. Which, of course, takes zero time.

like image 168
StefanKarpinski Avatar answered Nov 12 '22 20:11

StefanKarpinski


You probably want to use time_ns function in Julia:

function f()
    i = 0
    t1 = time_ns()
    while true
        i += 1
        if time_ns() - t1 >= 10^9
            break
        end
    end
    return i
end

On my computer it runs 10x faster than Python.

like image 5
Bogumił Kamiński Avatar answered Nov 12 '22 21:11

Bogumił Kamiński