Re: unexpected Timing results
- To: mathgroup at smc.vnet.net
- Subject: [mg115831] Re: unexpected Timing results
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Sat, 22 Jan 2011 03:21:16 -0500 (EST)
gleam at flashmail.com wrote: > I experienced a couple of rather unexpected timing results today that > I am hoping can be explained to me. > > In Mathematica 7.0.1: > > ragged = RandomInteger[1*^6, #] &~Array~1000; > > Do[PadLeft[ragged], {50}] // Timing > Do[PadLeft[ragged, {1000, 1000}], {50}] // Timing > Do[PadLeft[#, 1000] & /@ ragged, {50}] // Timing > > {4.422, Null} > {4.406, Null} > {0.344, Null} > > This difference I did not anticipate. I suppose this has something > to do with packed arrays, but I do not understand why the upper two > forms do not handle the task at least as well as the third one. I > expected a function that can be applied at the top level to be faster > that way, than when manually mapped. > > Now, truly baffling to me is this: > > padded = PadLeft[#, 1000] & /@ ragged; > > Do[Accumulate[padded], {800}] // Timing > Do[Map[Accumulate, padded, {0}], {800}] // Timing > > {5.109, Null} > {4.188, Null} > > Should not these forms be identical, other than a slight overhead for > the Map function? How can Map[f, x, {0}] be faster than f[x] ? I > was sure this timing was a fluke, so I restarted Mathematica and > repeated it several times, and the result held. > > Paul Same answer for both pairsadoxes. For both arrays 'ragged' and 'padded', he individual rows are packed arrays but the full array is not. So operations that Map over the (packed) rows can be faster than equivalent operations on the entire (not packed) structure. If you do padded = Developer`ToPackedArray[padded]; then you will see the last two timings more in line with expectation (second one ever so slightly slower than first). Daniel Lichtblau Wolfram Research