Why does this Python program run 3x faster than an identical Julia program -
i wanted try julia programming language, heard supposed faster python. decided try dynamic program project euler #14, dealt lot of computation (finding longest collatz sequence).
i wrote program in julia, , decided try make analogous program in python, see how fast each of them were. however, on machine, python program runs in 2 seconds, while julia 1 takes 7. surprised me, because said before julia claims faster python, wondering if there inherent scripts makes julia 1 slower. attempted have them both computed in same way.
here scripts, appreciate insight!
julia:
global max_val = 0 global max_item = 0 global d = dict{integer,integer}() function coll_length(n) #println(n) global max_val global max_item global d if haskey(d,n) return d[n] end if n==1 answer = 1 elseif n%2==1 answer = 1+coll_length(3n+1) else answer = 1+coll_length(n/2) end d[n]=answer if max_val<answer max_val=answer max_item=n end return answer end = 1:1000000 coll_length(i) end println(max_item)
python:
d = {} max_val = 0 max_item = 0 def coll_length(n): global d global max_val global max_item if n in d: return d[n] if n==1: answer= 1 elif n%2==0: answer= 1+coll_length(n/2) else: answer= 1+coll_length(3*n+1) d[n]=answer if max_val<answer: max_val=answer max_item=n return answer n in range(1,1000000+1): coll_length(n) print max_item
Comments
Post a Comment