MathGroup Archive 2004

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

Search the Archive

Re: Re: Counting Runs


>> 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
http://www.akikoz.net/~andrzej/
http://www.mimuw.edu.pl/~akoz/


  • 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