Last active
January 3, 2016 11:49
-
-
Save milktrader/8458812 to your computer and use it in GitHub Desktop.
Turbo boosting the moving method with Iterators (h/t Slender Means)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
julia> using Series, MarketData, FactCheck | |
julia> @time fast = fastmoving(Cl, mean, 60); | |
elapsed time: 0.305857977 seconds (72646744 bytes allocated) | |
julia> @time slow = moving(Cl, mean, 60); | |
elapsed time: 4.925385985 seconds (2079575504 bytes allocated) | |
julia> @fact fast[end].value => roughly(slow[end].value) | |
Success :: :(fast[end].value) => :(roughly(slow[end].value)) | |
julia> 4.925385985/0.305857977 | |
16.103506710240225 |
The array version even seems to be faster. (Though for some reason more memory-intensive?)
julia> @time map(mean, apartition(randn(50_000), 60))
elapsed time: 0.010084782 seconds (3430204 bytes allocated)
julia> @time map(mean, partition(randn(50_000), 60))
elapsed time: 0.022321433 seconds (5587344 bytes allocated)
One last comment (hopefully). You may get a slight-to-moderate speedup from just calling map
on the partitions, instead of collect(imap(f, partition(...)))
. If you're just going to collect the entire IMap
right away, you might as well just use map
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is pretty easy to fix. If you look at the
next
method for Partition, you'll see it initially makes arrays of the partitions, but then converts them to a tuple at the end.If you make a version of this type, say
APartition
, which is exactly the same, but simply removes that tuple conversion, thenkurtosis
, and any function that requires an Array should work.mean
works because there is a very genericmean(iteratable)
method that works with the tuple partitions.Really, it's not 100% clear why having partition use tuples instead of arrays is better, especially given the limited number of functions that can dispatch on arbitrary tuples. Maybe that's an issue/PR for the Iterators repo.