An alternative to scale! for SubArray objects
I have heard that SubArray
other array operations will eventually have to happen with respect to future overhauls . At the moment I have had some minor slowdowns in my code due to being slow SubArray
. Here's an example:
A=rand(Float32,20000,20000);
B=sub(A,2:19999,2:19999);
@time scale!(A,0.2f0);
@time scale!(B,0.2f0);
resulting in
elapsed time: 0.245619038 seconds
elapsed time: 11.706939438 seconds
and therefore scale!
about 40-50 times slower at level SubArray
than Array
.
Is there a simple workaround that I can use for scale!
for SubArray
now? Or should I wait for changes in the next release?
If there are no workarounds, this is not a big problem, I was just curious.
source to share
If you take advantage of this, it won't be particularly difficult to radically improve performance. scale!
you just need an implementation that uses cartesian (multidimensional) indexing rather than linear indexing. For now, this means that you should use what may be unfamiliar syntax Base.Cartesian
.
Below is the kernel code for scale!
. You would like it to look more like this implementationcopy!
. The function body would literally change two characters (adding s*
this assignment to the RHS).
Of course, if you want to stick with Julia 0.3, it's best to use your own implementation.
source to share