随笔 - 809  文章 - 0 评论 - 144 阅读 - 770万

复制代码
unit Unit14;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.StrUtils, Vcl.StdCtrls;

type
  TForm14 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form14: TForm14;

implementation

{$R *.dfm}

uses QString;

function GetStrNum(const SubStr, S: string): Integer;
var
  I: Integer;
begin
  Result := 0;
  I := PosEx(SubStr, S, 1);
  if I > 0 then
    Inc(Result)
  else
    Exit;

  while (I > 0) and (I < Length(S)) do
  begin
    I := PosEx(SubStr, S, I + 1);
    if I > 0 then
      Inc(Result);
  end;
end;

function StrPostime(Str:string;P:string):integer;
var i,n:integer;
begin
n:=0;
for i:=1 to length(str) do
begin
if copy(str,i,length(p))=P then n:=n+1;
end;
StrPostime:=n;
end;

function SubStrConut(mStr: string; mSub: string): Integer;
{ 返回子字符串出现的次数 }
begin
  Result :=
    (Length(mStr) - Length(StringReplace(mStr, mSub, '', [rfReplaceAll]))) div
    Length(mSub);
end; { SubStrConut }

function StrPosCount(subs:string;source:string):integer;
var
Str : string;
begin
Result := 0;
str := source;
while Pos(Subs,Str)<>0 do
begin
Delete(Str,Pos(Subs,Str),Length(Subs));
Inc(Result);
end;
end;


function InStrTimes(const AShortStr,ASourceStr: string): Integer;
var
  MyArrS: TArray<string>;
begin
  MyArrS := ASourceStr.Split([AShortStr], ExcludeEmpty);
  Exit(Length(MyArrS)-1);
end;

procedure TForm14.Button1Click(Sender: TObject);
var
  n: Cardinal;
  I: Integer;
begin
  //------------------------------------
  n := GetTickCount;
  for I := 0 to 1000000 do
  begin
    LeftStrCount('我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗','爱你的',false);
  end;
  n := GetTickCount - n;
  Text := IntToStr(n) + ' - ';


  //------------------------------------
  n := GetTickCount;
  for I := 0 to 1000000 do
  begin
    RightStrCount('我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗','爱你的',false);
  end;
  n := GetTickCount - n;
  Text := Text + IntToStr(n) + ' - ';


  //------------------------------------
  n := GetTickCount;
  for I := 0 to 1000000 do
  begin
    GetStrNum('爱你的','我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗');
  end;
  n := GetTickCount - n;
  Text := Text + IntToStr(n) + ' - ';


  //------------------------------------
  n := GetTickCount;
  for I := 0 to 1000000 do
  begin
    StrPostime('我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗','爱你的');
  end;
  n := GetTickCount - n;
  Text := Text + IntToStr(n) + ' - ';


  //------------------------------------
  n := GetTickCount;
  for I := 0 to 1000000 do
  begin
    SubStrConut('我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗','爱你的');
  end;
  n := GetTickCount - n;
  Text := Text + IntToStr(n) + ' - ';


  //------------------------------------
  n := GetTickCount;
  for I := 0 to 1000000 do
  begin
    StrPosCount('爱你的','我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗');
  end;
  n := GetTickCount - n;
  Text := Text + IntToStr(n) + ' - ';

  //------------------------------------
  n := GetTickCount;
  for I := 0 to 1000000 do
  begin
    InStrTimes('爱你的','我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗');
  end;
  n := GetTickCount - n;
  Text := Text + IntToStr(n);

end;

end.
复制代码

 

 

可见第三个是最快的。

隔离出来第三个备份下:

复制代码
function InStrTimes(const ASubStr,ASourceStr: string): Integer;
var
  I: Integer;
begin
  Result := 0;
  I := PosEx(ASubStr, ASourceStr, 1);
  if I > 0 then
  begin
    Inc(Result);
  end else begin
    Exit;
  end;
  while (I > 0) and (I < Length(ASourceStr)) do
  begin
    I := PosEx(ASubStr, ASourceStr, I + 1);
    if I > 0 then
    begin
      Inc(Result);
    end;
  end;
end;
复制代码

 

posted on   del88  阅读(210)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2013-03-23 TstringList 新发现 DelimitedText和CommaText
2012-03-23 Adoquery Locate 查询-(2)
点击右上角即可分享
微信分享提示