       Re:Re: Counting Runs

• To: mathgroup at smc.vnet.net
• Subject: [mg52212] Re:[mg52170] Re: Counting Runs
• From: "Fred Simons" <f.h.simons at tue.nl>
• Date: Sun, 14 Nov 2004 20:15:09 -0500 (EST)
• References: <200411130940.EAA01037@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Dear Carl,

Yes, I remember the problem how to count how many 0's are followed by a 1.
Allan Hayes and you had some wonderful ideas how to solve that.

With respect to your question, it turns out that using 1-Abs[Sign[data-int]]
is slightly faster. But when we use Abs[Sign[data-int] and then count how
may 1's are followed by a 0 there is a considerable gain.

In:=
runs[int_,data_]:=
Module[{modlist},modlist=Abs@Quotient[#,#+1,1]&@Abs[data-int];
Tr[BitXor[modlist,RotateRight[modlist]]]/2+BitAnd[modlist[],modlist[[-1]]
]]

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

In:=
runs2[int_,data_] :=
With[{lst=Abs[Sign[data-int]]},
Total[ BitXor[lst ,RotateLeft[lst]]]/2+
If[lst[]\[Equal]1, 0,If[lst[[-1]]\[Equal]0, 1,0]] ]

In:=
data=Table[Random[Integer,20], {2 10^7}];

In:=
n=5;{Do[runs[3, data], {n}]// Timing, Do[runs1[3, data], {n}]// Timing,
Do[runs2[3, data], {n}]// Timing}
Out=
{{7.516 Second,Null},{6.328 Second,Null},{4.109 Second,Null}}

Regards,

Fred Simons
Eindhoven University of Technology

```

• Prev by Date: Re: Plot and axes
• Next by Date: Re: Plot and axes
• Previous by thread: Re: Challenge: Fastest method to convert positive integers to 1 in a long list
• Next by thread: Re: Challenge: Fastest method to convert positive integers to 1 in a long list