Let me explain. The easy place to add an in-place operation with resize!
would be with the RNG call, rpois. I used resize! to make the Poisson RNG
go a little faster. It's now:
function rpois!(n::Int,p::Vector{Float64},out::Vector{Int})
resize!(out,n)
for i in 1:n
@inbounds out[i] = StatsFuns.RFunctions.poisrand(p[i])
#rand(Poisson(p[i]))
end
end
and then I change the script to use that. However, it doesn't help very
much, the time IN the RNG calls is still the major timing consuming
element, and the allocation wasn't much of a factor.
The other place that has an allocation is in the R.rep function. I am not
sure how to make that an in-place function though. For reference, this
function takes an array like [0.1 0.2 0.3] and an array of ints [2 3 1] and
duplicates each element that many times: [0.1 0.1 0.2 0.2 0.2 0.3]. With it
being in-place, you don't have an easy way to reference what values should
be duplicated how many times. In this case, the allocation is a (pretty
small, but still shows up) measurable part of the timing, but is harder to
deal with.
So we're stuck with RNG time as the major issue, but can chop off a little
more if this allocation can be dealt with better (the R code has both of
these same issues, which is why we're virtually tied).
On Thursday, July 21, 2016 at 5:49:23 AM UTC-7, Steven G. Johnson wrote:
>
>
>
> On Thursday, July 21, 2016 at 5:37:12 AM UTC-4, Chris Rackauckas wrote:
>>
>> Maybe. I thought about that, but I don't think that satisfies the
>> "elegant and compactness" requirement, unless there's an easy way to do the
>> growing without too much extra code hanging around.
>>
>
> Why is `resize!` so much harder than allocating a new array?
>