Pre-allocate DateTime Array - How?


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
where df is the DateFormat object
Thanks for the help so far.


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)


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.


  • 14.715 s
  • 14.794 s
  • 14.741 s


  • 14.92 s
  • 14.964 s
  • 14.929 s