易截截图软件、单文件、免安装、纯绿色、仅160KB

《Delphi 算法与数据结构》: 关于 const

如果参数在函数中不可能修改, 一定要使用 const;
不然, 编译器就会:
假定先修改, 先要备份; 使用前后要增减引用计数; 还要套上 try finally.
指定了 const 就可以避免以上过程从而提高效率.
unit
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class
(TForm)
Button1: TButton;
procedure
Button1Click(Sender: TObject);
end
;
var
Form1: TForm1;
implementation
{$R *.dfm}
//判断一个字符串中数字的个数
function
GetNum1(str: string
): Integer;
var
i: Integer;
begin
Result := 0
;
for
i := 1
to
Length(str) do
if
str[i] in
['0'
..'9'
] then
Inc(Result);
end
;
//同样的函数只是给参数加上 const
function
GetNum2(const
str: string
): Integer;
var
i: Integer;
begin
Result := 0
;
for
i := 1
to
Length(str) do
if
str[i] in
['0'
..'9'
] then
Inc(Result);
end
;
{对比测试}
procedure
TForm1.Button1Click(Sender: TObject);
var
s: string
;
n: Cardinal;
i: Integer;
begin
s := 'ABC123'
;
n := GetTickCount;
for
i := 0
to
1000000
do
GetNum1(s);
n := GetTickCount - n;
Text := IntToStr(n) + ' - '
;
n := GetTickCount;
for
i := 0
to
1000000
do
GetNum2(s);
n := GetTickCount - n;
Text := Text + IntToStr(n);
end
;
end
.


相关文档:

Delphi中实现程序的延时的方法

在Delphi中,通常可以用以下三种方法来实现程序的延时,即TTtimer控件,Sleep函数,GetTickCount函数。但是其精度是各不相同的。
一、三种方法的简单介绍
1)TTtimer控件
  TTtimer控件的实质是调用Windows API定时函数SetTimer和KillTimer来实现的,并简化了对WM_TIMER 消息的处理过程。通过设置OnTimer事
件和Inte ......

Delphi简单U盘传染病毒

program Project1;
{$APPTYPE CONSOLE}
uses
   windows, Tlhelp32,   SysUtils;
//===========================获得系统目录=======================================
function GetWinDir: string;
var
   Buf: array[0..MAX_PATH] of char;
begin
   GetSystemDirector ......

Delphi Register Test

var
Form1: TForm1;
a, b, c: Integer;
implementation
{$R *.dfm}
procedure test1(x, y, z: integer);
asm
mov a,eax
mov b,edx
mov c,ecx
end;
procedure test2(x, y, z: integer);
var
i,j,k: integer;
asm
mov i,eax
mov j,edx
mov k,ecx
mov eax,[esp+8]
mov a,eax
mov ......

《Delphi 算法与数据结构》: 数据对齐

为了加快硬件的访问速度, 编译器通常要使用"数据对齐", 譬如:
//下面结构中: SizeOf(TRec) = 6; 因为 b 在这里也要占 2 字节.
TRec = record
a: Word;
b: Byte;
c: Word;
end
;
//下面结构中: SizeOf(TRec) = 16; 这里的 a 和 b 共占了 8 个字节.
TRec = record
a: Byte;
b: Byte;
c: Do ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号