It is hard to test my assumptions when you don't post a complete runnable 
example, and profiling <http://docs.julialang.org/en/latest/stdlib/profile/> 
is a very useful tool in situations like this. It is also a little unclear 
what you mean by compile time, as Julia compiles the code while executing 
(JIT compiling).

After a simple reading of the posted code the following line stood out .

for i = 1:npop, j=1:npop 
    affinity += beta[x[i], x[j]] 
end 

This loop runs for 250 * 500 iterations and allocates a 500 * 500 array on 
each iteration (because for some reason Julia has decided that  x += y is 
not a special in place increment operation but just a short alias for x = x 
+ y).  It seems to me that this could be written in a much more efficient 
way without creating temporary arrays.

Another thing that might cause performance grief is that you have 

cut :: Vector{FloatingPoint}
# Instead of 
cut::Vector{Float64}

FloatingPoint is a abstract type that lets you store different floating 
point types in the array (like Float16, Float32, Float64, Float128 and 
BigFloat). Unfortuenatly that means that for all operations on the array 
Julia needs to check what type the element has and find the right method 
for the operation. That is much slower than when it compile time knows the 
type to be Float64

The last line:

fill!(actors, Actor()) 

Seems like a bug, because the actors array will be filled with pointers to 
the same Actor object, and if you change one of them, all will be changed.

Regards Ivar


kl. 09:01:12 UTC+2 lørdag 26. juli 2014 skrev Ross Boylan følgende:
>
> In julia 0.3, my module includes 
> const npop = 500  #population size 
> This takes a very noticeable (1 minute?) amount of time to compile. 
>
> If I set const npop=50 instead, compilation is instant (i.e., no 
> perceptible delay). 
>
> The module does create some Arrays that have npop as one of their 
> dimensions.  Some are 500 x 40. 
>
> Is this behavior expected? 
>
> I might believe different code is generated when npop is very small, but 
> for 50 is already well over what I'd call small. 
>
> Ross Boylan 
>
> P.S. the module level variables involving npop are (with nstep=40, 
> n1=250) 
>
> counts = zeros(Int, 3, nstep) 
>
> affinity = randn(npop, npop)  # will be modified later 
>
> x = [ i<=n1 ? 1 : 2 for i in 1:npop] 
>
> beta = [0.5 -0.5; -0.5 0.5] 
>
> for i = 1:npop, j=1:npop 
>     affinity += beta[x[i], x[j]] 
>     end 
>
> type Actor 
>     cut :: Vector{FloatingPoint}  # track cut point for each time 
>     matches :: Matrix{Int}  # 2xtime successful matches with each type 
> end 
>
> Actor() = Actor(2ones(FloatingPoint, nstep), zeros(Int, 2, nstep)) 
>
> actors = Array(Actor, npop) 
> fill!(actors, Actor()) 
>
> I also specify the random seed, and so in principle everything could be 
> set up at compile time.   
>
>

Reply via email to