I am a frequent user of Python for ML and am interested in converting to Julia for its promising improvements in speed. To get a feel for this I have just written a simple loop in both Python and Julia in order to compare the runtime for each one, but I seem to be getting unexpected results in the Julia implementation.
Here are both versions:
In Julia:
B = Array{Float64, 2}(undef, 10000,10000)
function f(x)
return x^2
end
function g(x)
return x^3
end
function h(x)
return x^4
end
function compose(f,g,h,x)
return f(g(h(x)))
end
for i in 1:10000
for j in 1:10000
B[i,j] = compose(f,g,h,i j)
end
end
println(sum(B))
In Python:
import numpy as np
B = np.zeros((10000, 10000), dtype=np.float64)
def f(x):
return x**2
def g(x):
return x**3
def h(x):
return x**4
def compute(x):
return f(g(h(x)))
for i in range(10000):
for j in range(10000):
B[i, j] = compute(i j 2)
print(np.sum(B))
EDIT: The Julia version returns ~8.3e22 while Python returns ~1.0e109 (which is what I expect).
While the Julia version runs extremely fast compared to the Python version, its results aren't what I expect them to be. As mentioned above, I am using Julia for the first time here, but I can't see where my mistake might be. Is this a result of my array/datatype handling? Are my functions not doing what I think they are?
CodePudding user response:
I'm not sure how you compared the two codes, but on my machine Julia is simply 1000X faster.
f(x) = x^2
g(x) = x^3
h(x) = (x^2)^2
compute(x) = f(g(h(x)))
function compute_sum(B)
for j in 1:10000
for i in 1:10000
B[i,j] = compute(i j 0.0)
end
end
sum(B)
end
B = zeros(10000, 10000)
@time compute_sum(B)
1.0337869071875959e109
0.126618 seconds (1 allocation: 16 bytes)
And Python:
from time import time
t0 = time()
for i in range(10000):
for j in range(10000):
B[i, j] = compute(i j 2)
s = np.sum(B)
print('Time:', time()-t0, 'sec')
print(s)
Time: 126.33235883712769 sec
1.0337869071875948e 109
CodePudding user response:
I think one can even get that a little faster by omitting the B
and using the iterator feature of the sum
f(x) = x^2
g(x) = x^3
h(x) = (x^2)^2
compute(x) = f(g(h(x)))
large_sum(n) = sum(compute(i j 0.0) for i=1:n, j=1:n)
@time large_sum(10000)
yields
0.115042 seconds
1.0337869071880225e109
and
julia> @allocated large_sum(10000)
0
CodePudding user response:
Your integer values are too large - beyond 64 bits and overflows.
Use Float64
instead (or BigInt
):
compose(f,g,h,Float64(i j))