《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中,通常可以用以下三种方法来实现程序的延时,即TTtimer控件,Sleep函数,GetTickCount函数。但是其精度是各不相同的。
一、三种方法的简单介绍
1)TTtimer控件
TTtimer控件的实质是调用Windows API定时函数SetTimer和KillTimer来实现的,并简化了对WM_TIMER 消息的处理过程。通过设置OnTimer事
件和Inte ......
第一节 BDE、ADO、InterBase和dbExpress
Delphi中处理数据库主要有两种方法,也就是BDE、ADO,从Delphi 6.0开始还加入了一种dbExpress方法。 另外,Delphi还提供了专门处理Borland 公司自己的数据库产品InterBase 数据库的专门的方法。
BDE(Borland Databas Engine), 是Delph ......
(一) 使用动态创建的方法
首先创建 Excel 对象,使用ComObj:
var ExcelApp: Variant;
ExcelApp := CreateOleObject( 'Excel.Application' );
1) 显示当前窗口:
ExcelApp.Visible := True;
2) 更改 Excel 标题栏:
ExcelApp.Caption := '应用程序调用 Microsoft Excel';
3) 添加新工作簿:
ExcelApp.WorkBooks.Add ......