Hi John,
Actually I want to put 0-1000 into a n by n matrix, and I want each entry to be
a array containing some of these numbers from 1 to 1000. Given an pair (i,j), I
want to get an array B[i,j], so I think cell is a better way to do it than
putting arrays in arrays (like [[],[],…]).
And I want to use B[2,3][i] to calculate something, and it seems better if I
use the second method(creating a list T rather than use x), when i bigger than
512, in first method, both 2 steps requires allocations, but in the second one,
the second @time doesn’t require allocation.
@time x=B[2,3][i]
@time y=x^2+2*x+sqrt(x)
println(x)
@time T[i]=B[2,3][i]
@time y=T[i]^2+2*T[i]+sqrt(T[i])
println(T[i])
This problem really frustrated me, and I really appreciate your advice. The
code is for your convenience:
function test()
B=fill!(cell(5,5),Int64[])
for i=1:513
push!(B[2,3],i)
end
T=zeros(Int64,513)
y=0.0
for i=1:513
@time T[i]=B[2,3][i]
@time y=T[i]^2+2*T[i]+sqrt(T[i])
println(T[i])
end
for i=1:513
@time x=B[2,3][i]
@time y=x^2+2*x+sqrt(x)
println(x)
end
end
Yijing
> On Dec 1, 2014, at 8:49 PM, John Myles White <[email protected]> wrote:
>
> Hi Yijing,
>
> Thanks for the update. I'm a little confused by somethings happening in your
> code.
>
> (1) Do you really need to use cell(5, 5)? In general, you'll want to use more
> tightly typed arrays.
>
> (2) Do you really want to fill the B array with many copies of the exact same
> pointer? This seems improbable. Did you perhaps want to create a new Int64[]
> array for every element of B?
>
> -- John
>
> On Dec 1, 2014, at 6:45 PM, Yijing Wu <[email protected]
> <mailto:[email protected]>> wrote:
>
>> Sorry it seems that I simplified my problem too much in the previous code.
>> Actually I am running something similar to the following, and I run it as a
>> function and it starts to have allocation since i=512. I am really unsure if
>> it's my coding problem(so it can be improved to 0 bytes) or it is natural in
>> the system that allocations appear. Thank you so much!
>>
>> function test()
>> B=fill!(cell(5,5),Int64[])
>>
>> for i=1:513
>> push!(B[2,3],i)
>> end
>>
>> T=zeros(Int64,513)
>>
>> for i=1:513
>> @time T[i]=B[2,3][i]
>> println(T[i])
>> end
>> end
>>
>>
>> 在 2014年12月1日星期一UTC-6下午7时24分31秒,John Myles White写道:
>> Did you run this inside a function? If not, your results are not going to be
>> useful indicators of how code will perform inside a function.
>>
>> Inside of a function body, I see 0 bytes being allocated.
>>
>> -- John
>>
>> On Dec 1, 2014, at 4:53 PM, Yijing Wu <[email protected] <javascript:>>
>> wrote:
>>
>>> Hi all, I found a strange problem about a very simple code in julia and
>>> hopefully I can get some help from you, here is the code:
>>>
>>>
>>> B=[1:1000]
>>> T=zeros(Int64,1000)
>>> for i=1:1000
>>> @time T[i]=B[i]
>>> println(T[i])
>>> end
>>>
>>> And when I run the code, the @time shows that it require 48 bytes
>>> allocation when i is larger than or equal to 512, and 0 bytes when smaller.
>>> Is this a problem that can be improved or I have to accept that it is
>>> designed to take some allocations when larger than 512?
>>>
>>> Thanks a lot for your help!
>>>
>>
>