My code currently calls the UNIX &quot;du&quot; command to get the size of a directory structure:<div><div>        $size = `/usr/bin/du -sk $DATA_DIR | cut -f1`;</div></div><div><br></div><div>Knowing that shells are CPU time expensive and generally not portable across platforms I am looking into replacing it with a pure perl implementation:</div>

<div><div>        find( sub { -f and ( $size += -s _ ) }, $DATA_DIR );</div><div><br></div><div>Wanting to be able to brag about the speed increase, I timed them with the Benchmark routines, and got a shock when I tested against my /tmp directory:</div>

<div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">           Rate Internal Shell_du</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Internal 11.6/s       --     -99%</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Shell_du 1538/s   13123%       --</font></div></div><div><br></div><div>WOW!  The shell to du was 13 TIMES faster than the internal find code.  (FYI, the /tmp/ directory has 349MB across 6400 files.)</div>

<meta http-equiv="content-type" content="text/html; charset=utf-8"><div><br></div><div>As a test, I created a very small directory structure (12 files, 2 sub-directories, 120KB) and the results for 10,000 timings are opposite:</div>

<div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">           Rate Shell_du Internal</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Shell_du 1664/s       --     -68%</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Internal 5208/s     213%       --</font></div></div><div><br></div><div>This time the internal code was faster...</div><div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>

My test system is a CentOS 5.5 64-bit (2GB RAM, mostly free RAM used for caching), with Perl 5.8.8, and the /tmp filesystem is an EXT3.</div><div><br></div><div>This bit of code isn&#39;t time critical and the actual data that will be processed is closer to the 120K test case, so I may continue and remove the shell/du line, but I&#39;d like to know how this got so slow!</div>

</div><div><br></div><div>Dan</div><div><br></div><div>Just in case I made a blunder, here&#39;s the test code:</div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">#!/usr/bin/perl -w</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">use strict;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">use Benchmark qw(:all);</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">use File::Find;</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">my $foo               = 0;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">my $count             = shift || 2000;</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">my $DATA_DIR          = shift || &quot;/tmp&quot;;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">sub shell_du {</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        my $size = 0;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        $size = `/usr/bin/du -sk $DATA_DIR | cut -f1`;</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        chomp $size;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        return $size;</font></div><div>

<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">}</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">sub internal_du {</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        my $size = 0;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        find( sub { -f and ( $size += -s _ ) }, $DATA_DIR );</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        return $size;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">}</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>

</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">cmpthese ($count, {</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        &#39;Shell_du&#39; =&gt; sub { $foo = shell_du();    },</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        &#39;Internal&#39; =&gt; sub { $foo = internal_du(); },</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">});</font></div>

</div><div><br></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>-- </div>***************** ************* *********** ******* ***** *** **<br>&quot;Quis custodiet ipsos custodes?&quot;<br>    (Who can watch the watchmen?)<br>

    -- from the Satires of Juvenal<br>&quot;I do not fear computers, I fear the lack of them.&quot;<br>    -- Isaac Asimov (Author)<br>** *** ***** ******* *********** ************* *****************<br>
</div>