MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Counting Runs

  • To: mathgroup at
  • Subject: [mg52228] Re: [mg52194] Re: Counting Runs
  • From: Andrzej Kozlowski <akoz at>
  • Date: Mon, 15 Nov 2004 03:17:41 -0500 (EST)
  • References: <001601c4c8d4$6aecc3e0$6400a8c0@Main> <> <006b01c4c998$29b9a220$6400a8c0@Main> <>
  • Sender: owner-wri-mathgroup at

>> I think Abs, Tr, and BitXor are very fast because they are probably
>> optimized for integer input and the use of packed arrays.
> I don't think an optimization for integers can be effective if 
> functions take time to CHECK whether the array contains only integers. 
> Is that precomputed for packed arrays, somehow?
> Bobby

Yes, that's precisely what packed arrays do. Actually you can check 
precisely the effect of packed arrays in this case:

<< "Developer`"

runs[int_, data_] := Module[{modlist},
    modlist = Sign[Abs[data - int]];
     Tr[BitXor[modlist, RotateRight[modlist]]]/2 + 1 -
      BitOr[modlist[[1]], modlist[[-1]]]]

runs1[int_, data_] :=
   Module[{modlist, dt = FromPackedArray[data]},
    modlist = FromPackedArray[Sign[Abs[dt - int]]];
     Tr[BitXor[modlist, RotateRight[modlist]]]/2 + 1 -
      BitOr[modlist[[1]], modlist[[-1]]]]

seq = Table[Random[Integer, 10], {10^6}];

Timing[runs[3, seq]]

{0.4299999999999997*Second, 83183}

Timing[runs1[3, seq]]

{0.9299999999999997*Second, 83183}

So just over 50% performance increase.

Andrzej Kozlowski
Chiba, Japan

  • Prev by Date: Re: TraditionalForm of expressions WITHOUT evaluating.
  • Next by Date: [LinearAlgebra] Re: Matrix Dot Product
  • Previous by thread: Re: Counting Runs
  • Next by thread: Re: Re: Counting Runs