abrash-black-book/14-03.html

226 lines
6.7 KiB
HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="vsisbn" content="1576101746" />
<meta name="vstitle" content="Michael Abrash's Graphics Programming Black Book, Special Edition" />
<meta name="vsauthor" content="Michael Abrash" />
<meta name="vspublisher" content="The Coriolis Group" />
<meta name="vspubdate" content="07/01/97" />
<meta name="vscategory" content="Web and Software Development: Game Development,Web and Software Development: Graphics and Multimedia Development" />
<title>Michael Abrash's Graphics Programming Black Book Special Edition: Boyer-Moore String Searching</title>
<meta name="chapter" content="14" />
<meta name="pages" content="267-268" />
</head>
<body>
<center>
<table border="1">
<tr>
<td>
<a href="14-02.html">Previous</a>
</td>
<td>
<a href="index.html">Table of Contents</a>
</td>
<td>
<a href="14-04.html">Next</a>
</td>
</tr>
</table>
</center>
<p>How fast <i>is</i> Boyer-Moore? Listing 14.1 is a C implementation of Boyer-Moore searching; Listing 14.2 is a test-bed program that searches up to the first 32K of a file for a pattern. Table 14.1 (all times measured with Turbo Profiler on a 20 MHz cached 386, searching a modified version of the text of this chapter) shows that this implementation is generally much slower than <b>REPNZ SCASB,</b> although it does come close when searching for long patterns. Listing 14.1 is designed primarily to make later assembly implemenmore comprehensible, rather than faster; Sedge&rsquo;s implementation uses arrays rather than pointers, is a great deal more compact and very clever, and may be somewhat faster. Regardless, the far superior performance of <b>REPNZ SCASB</b> clearly indicates that assembly language is in order at this point.</p>
<table width="100%">
<tr>
<td colspan="7">
<hr />
</td>
</tr>
<tr>
<th valign="bottom" align="left" width="30%"></th>
<th valign="bottom" align="left" width="10%">&ldquo;g;&rdquo;</th>
<th valign="bottom" align="left" width="10%">&ldquo;Yogi&rdquo;</th>
<th valign="bottom" align="left" width="10%">&ldquo;igoY&rdquo;</th>
<th valign="bottom" align="left" width="10%">&ldquo;Adrian&rdquo;</th>
<th valign="bottom" align="left" width="10%">&ldquo;Conclusion&rdquo;</th>
<th valign="bottom" align="left" width="20%">&ldquo;You don&rsquo;t know what you know&rdquo;</th>
</tr>
<tr>
<td colspan="7">
<hr />
</td>
</tr>
<tr>
<td valign="top" align="left">Searching approach</td>
<td valign="top" align="left">(16K)</td>
<td valign="top" align="left">(16K)</td>
<td valign="top" align="left">(16K)</td>
<td valign="top" align="left">(&lt;1K)</td>
<td valign="top" align="left">(16K)</td>
<td valign="top" align="left">(16K)</td>
</tr>
<tr>
<td valign="top" align="left">REPNZ SCASB on first char a(Listing 9.1)</td>
<td valign="top" align="left">8.2</td>
<td valign="top" align="left">7.5</td>
<td valign="top" align="left">9.7</td>
<td valign="top" align="left">0.4</td>
<td valign="top" align="left">7.4</td>
<td valign="top" align="left">8.1</td>
</tr>
<tr>
<td valign="top" align="left">REPNZ SCASB on least common char (Listing 9.2)</td>
<td valign="top" align="left">7.6</td>
<td valign="top" align="left">7.5</td>
<td valign="top" align="left">7.5</td>
<td valign="top" align="left">0.5</td>
<td valign="top" align="left">7.5</td>
<td valign="top" align="left">7.5</td>
</tr>
<tr>
<td valign="top" align="left">Boyer-Moore in C (Listing 14.1)</td>
<td valign="top" align="left">71.0</td>
<td valign="top" align="left">38.4</td>
<td valign="top" align="left">37.7</td>
<td valign="top" align="left">1.8</td>
<td valign="top" align="left">18.2</td>
<td valign="top" align="left">9.2</td>
</tr>
<tr>
<td valign="top" align="left">Standard Boyer-Moore in ASM(code not shown)</td>
<td valign="top" align="left">38.5</td>
<td valign="top" align="left">21.0</td>
<td valign="top" align="left">20.5</td>
<td valign="top" align="left">0.8</td>
<td valign="top" align="left">9.4</td>
<td valign="top" align="left">4.8</td>
</tr>
<tr>
<td valign="top" align="left">Quick handling of first mismatch Boyer-Moore in ASM(Listing 14.3)</td>
<td valign="top" align="left">14.1</td>
<td valign="top" align="left">8.9</td>
<td valign="top" align="left">7.7</td>
<td valign="top" align="left">0.4</td>
<td valign="top" align="left">4.0</td>
<td valign="top" align="left">2.0</td>
</tr>
<tr>
<td valign="top" align="left">&lt;=255 pattern length + sentinelBoyer-Moore in ASM(Listing 14.4)</td>
<td valign="top" align="left">8.1</td>
<td valign="top" align="left">5.2</td>
<td valign="top" align="left">4.6</td>
<td valign="top" align="left">0.3</td>
<td valign="top" align="left">2.6</td>
<td valign="top" align="left">1.2</td>
</tr>
<tr>
<td valign="top" align="left" colspan="7"><small>Search pattern (approximate distance searched before match is shown in parentheses).</small><br />
<small>Times are in milliseconds; shorter is better.</small></td>
</tr>
<tr>
<td colspan="7">
<hr />
</td>
</tr>
<tr>
<th colspan="7" caption="" align="left">Table 14.1 Comparison of searching techniques.</th>
</tr>
<tr>
<td colspan="7">
<hr />
</td>
</tr>
</table>
<p>The entry &ldquo;Standard Boyer-Moore in ASM&rdquo; in Table 14.1 refers to straight-forward hand optimization of Listing 14.1, code that is not included in this chapter for the perfectly good reason that it is slower in most cases than <b>REPNZ SCASB.</b> I say this casually now, but not so yesterday, when I had all but concluded that Boyer-Moore was simply inferior on the x86, due to two architectural quirks: the string instructions and slow branch. I had even coined a neat phrase for it: Architecture is destiny. Has a nice ring, doesn&rsquo;t it?</p>
<center>
<table border="1">
<tr>
<td>
<a href="14-02.html">Previous</a>
</td>
<td>
<a href="index.html">Table of Contents</a>
</td>
<td>
<a href="14-04.html">Next</a>
</td>
</tr>
</table>
</center>
<hr width="90%" size="1" noshade="noshade" />
<div align="center">
Graphics Programming Black Book &copy; 2001 Michael Abrash
</div>
</body>
</html>