<?xml version="1.0" encoding="windows-1252"?>
<node id="350120" title="Re: fast bit twiddling" created="2004-05-03 15:42:59" updated="2005-08-15 14:32:14">
<type id="11">
note</type>
<author id="343950">
emazep</author>
<data>
<field name="doctext">
Inspired by BrowserUK's 2nd algorithm &lt;strike&gt;but slightly faster, since it calls &lt;tt&gt;substr&lt;/tt&gt; just once in exchange for an extra assignment&lt;/strike&gt;:

&lt;code&gt;
sub emazep {
  my $s = shift;
  my $tmp;

  ( ($tmp = substr $s, $_-1, 2) eq '11' or $tmp eq '00' ) and return  for @_;

  return 1;
}
&lt;/code&gt;

Using exactly the same benchmark provided [id://349815|here] by BrowserUK, it gives:

&lt;code&gt;
          Rate   ysth  japhy matija   buk2   buk1 emazep
ysth    1966/s     --   -17%   -87%   -89%   -89%   -91%
japhy   2361/s    20%     --   -84%   -87%   -87%   -89%
matija 14719/s   648%   523%     --   -19%   -20%   -32%
buk2   18088/s   820%   666%    23%     --    -1%   -17%
buk1   18363/s   834%   678%    25%     2%     --   -16%
emazep 21753/s  1006%   821%    48%    20%    18%     --
&lt;/code&gt;
&lt;br&gt;
&lt;b&gt;Update:&lt;/b&gt;
&lt;br&gt;&lt;br&gt;
added esialb's (corrected) solution to the BrowserUK's benchamrk:

&lt;code&gt;
          Rate   ysth  japhy matija esialb   buk1   buk2 emazep
ysth    1975/s     --   -17%   -87%   -88%   -89%   -89%   -91%
japhy   2374/s    20%     --   -84%   -85%   -87%   -87%   -89%
matija 15000/s   659%   532%     --    -8%   -17%   -18%   -30%
esialb 16299/s   725%   586%     9%     --   -10%   -11%   -23%
buk1   18156/s   819%   665%    21%    11%     --    -1%   -15%
buk2   18277/s   825%   670%    22%    12%     1%     --   -14%
emazep 21295/s   978%   797%    42%    31%    17%    17%     --
&lt;/code&gt;

&lt;br&gt;
&lt;b&gt;2nd Update:&lt;/b&gt;
&lt;br&gt;&lt;br&gt;
I've been misleaded by the BrowserUK solution, which contains an error which also slowed down his sub (see [id://350569|here].) After the correction his sub proves to be the fastest (dani_l's very elegant solution also included in the benchmark):

&lt;code&gt;
          Rate   ysth  japhy esialb matija   buk1 dani_l emazep  buk2*
ysth    1845/s     --   -22%   -85%   -87%   -88%   -89%   -90%   -91%
japhy   2366/s    28%     --   -81%   -84%   -84%   -86%   -87%   -88%
esialb 12530/s   579%   430%     --   -14%   -16%   -28%   -31%   -38%
matija 14566/s   689%   516%    16%     --    -2%   -16%   -20%   -28%
buk1   14885/s   707%   529%    19%     2%     --   -14%   -18%   -27%
dani_l 17357/s   841%   634%    39%    19%    17%     --    -5%   -14%
emazep 18181/s   885%   669%    45%    25%    22%     5%     --   -10%
buk2*  20275/s   999%   757%    62%    39%    36%    17%    12%     --
&lt;/code&gt;

&lt;tt&gt;buk2*&lt;/tt&gt; is the buk2 sub as corrected by me [id://350569|here].
&lt;br&gt;
&lt;br&gt;
Cheers,
Emanuele.</field>
<field name="root_node">
349759</field>
<field name="parent_node">
349759</field>
</data>
</node>
