Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have found that multidimensional Reverse is ~10 times slower than exact -1 ;; 1 ;; -1:

n = 1000;
mat = RandomReal[1, {n, n}];

mat2 = Reverse[mat, 2]; // AbsoluteTiming
mat3 = mat[[All, -1 ;; 1 ;; -1]]; // AbsoluteTiming
(* {0.088234, Null} *)
(* {0.006836, Null} *)

mat2 == mat3
(* True *)

Why? Is it a bug?

Both produced arrays are packed.

I use Mathematica 9.0.1 on Linux. Mathematica 8 has the same problem.


Reply from the Wolfram Technical Support:

Thank you for your message.

I could reproduce this performance issue on Mathematica 9.

I have filed a report on this to our database and thank you for bringing this issue to our attention.


Edit: Mathematica 10.0.0 has the same problem...

share|improve this question
    
Neither of them seem to unpack the array. Maybe this is something that should be reported to support. –  Szabolcs Nov 25 '13 at 19:01
    
Results are the opposite for SparseArray objects –  belisarius Nov 25 '13 at 20:38

1 Answer 1

Mathematica is full of these idiosyncrasies and honestly I have no idea how one is to figure out why. Still I think it's an interesting question. One possibility is that Part was optimized at some point and Reverse got left behind. In my experience Part is highly optimized for packed arrays. At least in v7 Reverse is faster on unpacked data:

SetAttributes[timeAvg, HoldFirst]

timeAvg[func_] :=
  Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}]

mat = Developer`FromPackedArray @ mat;

Reverse[mat, 2];           // timeAvg
mat[[All, -1 ;; 1 ;; -1]]; // timeAvg
0.005872

0.009736
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.