一步步教你优化Delphi字串查找

本文介绍了在编写离线浏览器WebSeizer过程中,如何优化Delphi的字符串查找函数。通过对比Pos和自定义的RightPos、RightPosEx函数,展示了如何减少内存操作以提高效率。RightPosEx通过避免使用Delete和Pos,直接操作PChar来提升性能,实测结果显示其效率优于RightPos。
摘要由CSDN通过智能技术生成
本人在编写离线浏览器WebSeizer的过程中,用到大量字符串处理函数,这是很耗CPU的
一个处理过程,为了编写出高效率的网页解析引擎,对优化代码作了一番研究。
  1 、高精度的计时函数
  代码优化时需要用到精确的计时器。常用的有GetTickCount函数,可以达到毫秒级
的精度。但还是很不够的,这时可以采用提高循环次数的办法。另外,还有一个精度
更高的定时——“高分辨率性能计数器”(high-resolution performance counter),它提供了两个API
函数,取得计数器频率的QueryPerformanceFrequency和取得计数器数值
QueryPerformanceCounter。实现原理是利用计算机中的8253,8254可编程时间间隔定时器芯
片实现的。在计算机内部有三个独立的16位计数器。
  计数器可以以二进制或二—十进制(BDC)计数。计数器每秒产生1193180次脉冲,每次
脉冲使计数器的数字减一,产生频率是可变的,用QueryPerformanceFrequency可以得到,一般
情况下都是1193180。QueryPerformance Counter可以得到当前的计数器值。所以只要你的计
算机够快,理论上精度可以达到1/1193180秒。
  2 、代码优化实例
  下面以一个自定义的字符串函数的为例,说明代码优化过程。
  Delphi提供的字符串函数里有一个Pos函数,它的定义是:
function Pos(Substr: string; S: string): Integer;
  它的作用是在字符串S中查找字符串Substr,返回值是Substr在S中第一次出现的位置,如果没有
找到,返回值为0。
  在本人编写WebSeizer软件(天空软件站有下载)过程中,Pos已经不能满足要求。一方面:在
处理网页中的字符串时,要求对大小写不敏感,即
文件搜索控件 TEasyFileSearch v1.02 by Alexandre GAMBIER Read versions.txt file if you want to see modifications made between each version. Thanks ------ Thanks to : - Udo for his tests on C++ Builder 5 and his ideas for v1.01, v1.02 - J.M. Fontaine for his test on Delphi 7 and his orthography help for v1.01. 1 - DESCRIPTION --------------- TEasyFileSearch is freeware and you use it at your own risk. TEasyFileSearch is completely free also for commercial use. TEasyFileSearch is a component which enable you to search file or folder in directory and subdirectory, using filter mask (*.*, *.exe, ...). You define some search criteria : - look in subfolder , - look for readonly file , - look for hidden file , - look for system file , - look for archile file , - look for directory file, - look for anyfile file . You can filter your research - look for files that is smaller or equal to a size, - look for files that is bigger or equal to a size, - look for file than date is between two date, younger, older or the same than a date, - look for created, modified or opened files. You can exclude files using filter mask, for example you can exclude all files *.exe. TEasyFileSearch don't use recursive function (no stack owerflow error). 2 - LANGUAGE/OS --------------- TEasyFileSearch has been tested only on Delphi 5, 7 and C++ Builder 5. 3 - INSTALLATION ---------------- 1. Extract the TEasyFileSearch.zip 2. Choose Component|Install Component... from the menu 3. Select the Tab 'Into New Package' and fill in: At 'Unit file name': Browse and Select EasyFileSearchReg.pas from the directory where you installed it. At 'Package file name': TEasyFileSearch ( In the directory Projects\Lib ) At 'Description': TEasyFileSearch - looking for files Select Ok 4. You will be asked to confirm building the packag
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值