# Pre-allocate DateTime Array - How?

**URL:** <https://discourse.julialang.org/t/pre-allocate-datetime-array-how/1028>\
**Category:** New to Julia\
**Created:** [December 18, 2016, 5:49pm UTC](https://discourse.julialang.org/t/pre-allocate-datetime-array-how/1028 "2016-12-18T17:49:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_McConochie](https://avatars.discourse-cdn.com/v4/letter/j/b2d939/32.png) [@Jason\_McConochie](https://discourse.julialang.org/u/Jason_McConochie)\
**Post date:** [December 18, 2016, 5:49pm UTC](https://discourse.julialang.org/t/pre-allocate-datetime-array-how/1028/1 "2016-12-18T17:49:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 18, 2016, 10:44pm UTC](https://discourse.julialang.org/t/pre-allocate-datetime-array-how/1028/2 "2016-12-18T22:44:59Z")

</div>

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

```julia
sDate = Vector{DateTime}(100)

```

and update just using a for loop:

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

```

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [December 19, 2016, 12:56pm UTC](https://discourse.julialang.org/t/pre-allocate-datetime-array-how/1028/3 "2016-12-19T12:56:47Z")

</div>

Another, slightly faster option uses [map](http://docs.julialang.org/en/release-0.5/stdlib/collections/#Base.map).

```julia
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
