Pre-allocate DateTime Array - How?


#1

I have an array sDate::Array{DateTime,1}
I want to pre-allocate the memory for this - how?
Then I want to fill the array in a loop such as
sDate[i]=Dates.DateTime(tDate,df)
where df is the DateFormat object
Thanks for the help so far.
Jason


#2

You can pre-allocate just by using the Array constructor:

sDate = Vector{DateTime}(100)

and update just using a for loop:

for i in 1:100
  sDate[i] = DateTime(tDate, df)
end

#3

Another, slightly faster option uses map.

map((x) -> DateTime(x,df),tDate)

I compared map vs preallocation and a for loop using an array of timestamp strings 205560 x 1 in size.

map

  • 14.715 s
  • 14.794 s
  • 14.741 s

preallocation

  • 14.92 s
  • 14.964 s
  • 14.929 s