I am seriously eager to show others that Julia is fast, for its multiple dispatch.
But it is hard to prove, and below experiment shows that the python’s overloading is actually more faster than Julia’s multiple dispatch. You can compare the time consumed.
- Python’ overloading
from math import ceil
from time import time
def f_py(x):
if type(x) == str:
x = float(x)
if type(x) == float:
x = ceil(x)
return x**2 % 4
def apply_to_data(arr):
for a in arr:
f_py(a)
Test = ["1.5", 2.5, 3]
Repeat = [number for number in range(10**6, 10**8+1) if (number % 10**6 == 0)]
Ans = []
print("Python speed:")
for ind, N in enumerate(Repeat):
array = Test * N
t1 = time()
apply_to_data(array)
total = time()-t1
Ans.append(total)
if (ind % 10 == 0):
print(ind, round(total,3), " seconds")
- Julia’s Multiple Dispatch
# Dispatched Function
f(x::Int64) = x^2 % 4
f(x::Float64) = f(ceil(Int64, x))
f(x::String) = f(parse(Float64, x))
function apply_to_data_md(array)
for a in array
f(a)
end
end;
Test = ["1.5", 2.5, 3]
Repeat = [10^6:10^6:10^8+1;]
ans_time = []
ans_allo = []
for R in Repeat
array = [s for i=1:R for s in ["1.5", 2.5, 3]]
time = @timed apply_to_data_md(array)
push!(ans_time, time[2])
push!(ans_allo, time[3])
if R % 10^7 == 0
print(R, time[2])
end
end
I got the idea of above experiment from this article. But the result of mine is somewhat different from the post.
Do you know any other experiments more rigorously showing that the julia’s multiple dispatch could help the computational speed?