Dear all,
I guess I could use a piece of good help from the experts here. The fact is that I was about to translate a piece of Monte Carlo code from my old 0.6.4 program library, into Julia 1.0 (going through 0.7 first), until a friend of mine tried essentially the same code under Anaconda Python on the same computer. I was so disappointed by the results of test runs that I’m wondering what could be wrong on my code.
I post here the Julia and the Python codes:
Julia 0.6.4:
function TEST_ais(W,Nsamples)
pl_1 = Int32(1)
zer_0 = Int32(0)
pl_1 = 1
zer_0 = 0
log2 = log(2.0)
Nv, Nh = size(W) .- 1
Wt = transpose(W)
vvk = rand([zer_0,pl_1],Nsamples,Nv+1)
vvk[:,1] = pl_1
vv_k = rand([zer_0,pl_1],Nsamples,Nv+1)
vv_k[:,1] = pl_1
size_v = size(vv_k)
hhk = rand([zer_0,pl_1],Nsamples,Nh+1)
hhk[:,1] = pl_1
hh_k = rand([zer_0,pl_1],Nsamples,Nh+1)
hh_k[:,1] = pl_1
size_h = size(hh_k)
for i in 1:2
hh_k = zeros(Int32, size_h); hh_k[1.0./(1.0+exp.(-vv_k*W)) .> rand(size_h)] .= pl_1
vv_k = zeros(Int32, size_v); vv_k[1.0./(1.0+exp.(-hh_k*Wt)) .> rand(size_v)] .= pl_1
end
end;
W = rand(301,901)
tic()
TEST_ais(W,1024)
toc()
tic()
TEST_ais(W,1024)
toc()
Python:
import numpy as np
import sys, time
class TEST(object):
def __init__(self):
return
def TEST_ais(self,W,Nsamples):
pl_1 = 1
zer_0 = 0
log2 = np.log(2.0)
Nv = W.shape[0] - 1
Nh = W.shape[1] - 1
Wt = W.T
vvk = np.random.randint(zer_0,pl_1+1,[Nsamples,Nv+1])
vvk[:,1] = pl_1
vv_k = np.random.randint(zer_0,pl_1+1,[Nsamples,Nv+1])
vv_k[:,1] = pl_1
size_v = vv_k.shape
hhk = np.random.randint(zer_0,pl_1+1,[Nsamples,Nh+1])
hhk[:,1] = pl_1
hh_k = np.random.randint(zer_0,pl_1+1,[Nsamples,Nh+1])
hh_k[:,1] = pl_1
size_h = hh_k.shape
z1 = np.zeros(hhk.shape)
z2 = np.zeros(vvk.shape)
for i in range(5):
z1 = 1/(1+np.exp(-np.dot(vv_k,W)));
hh_k = np.zeros(size_h); hh_k[z1 > np.random.rand(size_h[0],size_h[1])] = pl_1
z2 = 1/(1+np.exp(-np.dot(hh_k,Wt)));
vv_k = np.zeros(size_v); vv_k[z2 > np.random.rand(size_v[0],size_v[1])] = pl_1
if __name__ == '__main__':
W = np.random.rand(301,901)
t = TEST()
itime = time.time()
t.TEST_ais(W,1024);
print(" took %.1fs" % (time.time()-itime))
sys.stdout.flush()
Now the fact is that, executing the julia code gives
julia < TEST.jl
elapsed time: 2.875419733 seconds
elapsed time: 1.55660307 seconds
while the Python version turns out to be faster
python TEST.py
took 2.0s
…and this is with the default python that comes with my ubuntu machine. Running the same code using the Anaconda python blows my mind up, giving
> python TEST.py
took 0.3s
I am quite surprised about the difference between the two python’s, but most horrified by the fact that the Anaconda python version is about 5.5 times faster than the Julia code
So I am sure I’m not optimizing my julia code, despite how simple it is. Can somebody here provide a hint about how to make the julia code as fast as the Anaconda one?
Thanks in advance for your kind help,
Ferran.