I mean you could skip the map and use a reduce directly thus skipping the array allocation.
You could skip the array allocation by using reduce instead of the map()... construct reduce((acc, x) -> promote_type(acc, typeof(x)),None, X) I created a Gist (https://gist.github.com/vchuravy/f25d31ef2e798ea21cc0) comparing different versions. Current Version: > elapsed time: 0.016431211 seconds (47688 bytes allocated) the map and expand version. Note the array allocation that is necessary because of map > elapsed time: 0.188875778 seconds (943688 bytes allocated) a version using reduce and an anonymous function > elapsed time: 0.0182863 seconds (49000 bytes allocated) a version using reduce without an anonymous function > elapsed time: 0.01681751 seconds (47688 bytes allocated) The remaining time differential between the current and the last function is that Julia currently can't specialize higher functions on functions given as argument. On Tuesday, 5 August 2014 00:02:30 UTC+2, Michael Grant wrote: > > Fair point. I suppose it is a bit optimistic, at this point, to expect the > compiler to perform the necessary temporary removal / stream fusion. > > On Monday, August 4, 2014 4:53:50 PM UTC-5, Tim Holy wrote: >> >> I suspect the main reason for the loop rather than map is simply to avoid >> the >> need to allocate a temporary. Since loops are fast in Julia, there is no >> downside to working element-by-element. >> >>
