在查看您的程序后,我发现了一些缺陷。为了了解进展情况,我首先测量了您现有程序在最坏情况下(始终将数字添加到0位置)的速度。
n:=500000;
for i:=0 to n-1
do Insert(i, 0);
测量结果:n=500000 47.6毫秒
A) 简单性
我从你的过程中删除了一些不必要的行(OldList是完全不必要的,SetLength会保留内存)。
改进A:
procedure Insert(_Value: Integer; _InsertPos: integer);
begin
if Length(FSortedList) < FCount + 1
then SetLength(FSortedList, FCount + 100);
Move(FSortedList[_InsertPos], FSortedList[_InsertPos+1], SizeOf(Integer) * (FCount - _InsertPos));
FSortedList[_InsertPos] := _Value;
Inc(FCount);
end;
速度提升 6% (44.8 毫秒)
B) 一切都计算在内
if Length(FSortedList) < FCount + 1
then SetLength(FSortedList, FCount + 100);
提示1:每次插入都会调用函数Length。
提示2:每次计算FCount+1。
提示3:过程参数应为const(按引用传递)。
提示4:引入FCapacity变量。
提示5:仅通过100增加长度会导致大量重新分配(在250万个数组上进行25,000次)。正如您所说,内存不是问题,那么为什么不预先分配全部或至少较大的空间呢?
procedure Insert(const _Value, _InsertPos: integer);
begin
if FCount = FCapacity
then begin
Inc(FCapacity, 100000);
SetLength(FSortedList, FCapacity);
end;
Move(FSortedList[_InsertPos], FSortedList[_InsertPos+1], SizeOf(Integer) * (FCount - _InsertPos));
FSortedList[_InsertPos] := _Value;
Inc(FCount);
end;
速度提升1% (44.3毫秒)。
提示:您可以实现一些渐进式算法,而不是递增100000。
C) 瓶颈
如果我们现在看这个过程,就什么也没剩下了,只有很多内存移动。如果我们无法改变算法,那么我们必须改进内存移动。
实际上还有快速移动挑战(fastcode.sourceforge.net)。
我准备了一个zip文件,里面只有你需要的那些文件(3个文件,源代码)。 链接 >>> http://www.dakte.org/_stackoverflow/files/delphi-fastcode.zip
- 将fastcodeCPUID.pas和fastmove.pas添加到您的项目中!
- 插入Uses fastmove.pas;
- 就这样!不用再做其他更改了!
在我的计算机上速度提升接近50%(取决于您使用的CPU)。
原始过程
n ms graph
---------------------------------
100000 1.8 *
200000 7.6 ***
300000 17.0 *******
400000 30.1 *************
500000 47.6 ********************
提高,不包括快速移动(-7%)
n ms graph
---------------------------------
100000 1.6 *
200000 6.9 ***
300000 15.7 ******
400000 28.2 ***********
500000 44.3 ******************
改进,使用 fastmove (-46%)
n ms graph
---------------------------------
100000 0.8 *
200000 3.8 **
300000 9.0 ****
400000 16.3 *******
500000 25.7 ***********
最新评论:
if FCount = FCapacity
then begin
if FCapacity<100000
then FCapacity:=100000
else FCapacity:=FCapacity*2;
SetLength(FSortedList, FCapacity);
end;
正如我所说,你可以增加一些渐进式的FCapacity增加。这是一种经典的Grow实现(如果需要,只需添加更多的if语句或将100000更改为更合适的值)。
D)更新2:数组作为^TArray
type
PIntegerArr3 = ^TIntegerArr3y;
TIntegerArr3y = array[0..1] of Integer;
var
FCapacity3,
FCount3: Integer;
FSortedList3: PIntegerArr3;
procedure ResizeArr3(var aCurrentArr: PIntegerArr3; const aNewCapacity: Integer);
var lNewArr: PIntegerArr3;
begin
GetMem(lNewArr, aNewCapacity*SizeOf(Integer));
if FCount3>0
then begin
if aNewCapacity<FCount3
then FCount3:=aNewCapacity;
Move(aCurrentArr^[0], lNewArr^[0], FCount3*SizeOf(Integer));
end;
FreeMem(aCurrentArr, FCapacity3*SizeOf(Integer));
FCapacity3:=aNewCapacity;
aCurrentArr:=lNewArr;
end;
procedure FreeArr3;
begin
if FCapacity3>0
then begin
FreeMem(FSortedList3, FCapacity3*SizeOf(Integer));
FSortedList3:=nil;
end;
end;
procedure Insert3(const _Value, _InsertPos: integer);
begin
if FCount3 = FCapacity3
then ResizeArr3(FSortedList3, FCapacity3 + 100000);
Move(FSortedList3^[_InsertPos], FSortedList3^[_InsertPos+1], SizeOf(Integer) * (FCount3 - _InsertPos));
FSortedList3^[_InsertPos] := _Value;
Inc(FCount3);
end;
C) 从步骤C中没有获得速度增益!
结论: 使用FastMove或算法更改后,“物理”内存移动速度的极限已经达到!
我正在使用Delphi XE3,在System.pas的第5307行:
(* ***** BEGIN LICENSE BLOCK *****
*
* The assembly function Move is licensed under the CodeGear license terms.
*
* The initial developer of the original code is Fastcode
*
* Portions created by the initial developer are Copyright (C) 2002-2004
* the initial developer. All Rights Reserved.
*
* Contributor(s): John O'Harrow
*
* ***** END LICENSE BLOCK ***** *)
procedure Move(const Source; var Dest; Count: NativeInt);
实际上,在Delphi中已经有一些Fastcode例程,但是包括直接从其网站下载的例程(或从我上面提供的链接)使差异最大,几乎达到了50%(奇怪)。
@Runner
的Improved Sliced Array implementation
,如果需要任何有关如何改进排序的输入。 - LU RD