Re: Re: Counting Runs

• To: mathgroup at smc.vnet.net
• Subject: [mg52228] Re: [mg52194] Re: Counting Runs
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Mon, 15 Nov 2004 03:17:41 -0500 (EST)
• References: <001601c4c8d4\$6aecc3e0\$6400a8c0@Main> <opsheakrjtiz9bcq@monster.cox-internet.com> <006b01c4c998\$29b9a220\$6400a8c0@Main> <200411140930.EAA14521@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```>> 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