Step by step to teach you to optimize Delphi string Lookup

Source: Internet
Author: User
Tags time interval

I am in the process of writing off-line browser Webseizer, use a lot of string processing functions, this is a CPU consumption of a processing process, in order to write an efficient web analytics engine, to optimize the code made a study.

1. High-precision timing function

A precise timer is needed for code optimization. Commonly used with GetTickCount functions, can achieve the accuracy of the millisecond level. But it is not enough, at this time can be used to improve the number of cycles. In addition, there is a more accurate timing-"High resolution performance counter" (high-resolution performance counter), which provides two API functions, Gets the queryperformancefrequency of the counter frequency and gets the QueryPerformanceCounter of the counter value. The implementation principle is realized by using the 8253,8254 programmable time interval timer chip in the computer. There are three separate 16-bit counters inside the computer.

Counters can be counted in binary or two-decimal (BDC). The counter produces 1,193,180 pulses per second, each pulse to reduce the number of counters, the frequency is variable, with queryperformancefrequency can be obtained, under normal circumstances are 1193180. Queryperformance counter can get the current counter value. So as long as your computer

Fast enough, theoretical accuracy can reach 1/1193180 seconds.

2, code optimization examples

The following is an example of a custom string function that describes the code optimization process.

The string function provided by Delphi has a POS function, which is defined as:

function Pos(Substr: string; S: string): Integer;

Its role is to find the string substr in the string s, the return value is the first occurrence of substr in S, if not found, the return value is 0.

In my writing Webseizer software (Sky software station has download) process, POS can not meet the requirements. On the one hand: when dealing with strings in a Web page, it is not sensitive to case, that is, the meaning of < H T > and <HTML> representatives is exactly the same. On the other hand: We also require a function where the return value is the last occurrence of substr in S, not the first occurrence. The following is an optimized code for this function.

function RightPos(const Substr,S: string): Integer;
var
 iPos: Integer;
 TmpStr:string;
begin
 TmpStr:=s;
 iPos := Pos(Substr,TmpStr); Result:=0;
 //查找Substr第一次出现位置
 while iPos<>0 do
 begin
  Delete(TmpStr,1,iPos+length(Substr)-1);
  //删除已经查找过的字符
  Result:=Result+iPos;
  iPos := Pos(Substr,TmpStr); //查找Substr出现位置
  if iPos=0 then break;
  Result:=Result+length(Substr)-1;
 end;
end;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.