下面的代码是二分搜索,求ff在数组PMyX中的位置,算法也许需要完善,这里先只讨论效率个人认为,在while中用double类型ff来比较(程序中注释3的位置)速度是很慢的,尽管这个代码运行的次数不是很多,数组的大小>1.5M(Len>1.5M)
奇怪的是:采用注释1的变量,在注释2中进行整形变换,将注释3的ff改为整形ffint后,居然整体速度慢了无数,似乎多了无数另外的开销!
声明:别说,其它代码有影响,也别说,效率测试有问题,总之从这里找原因!
这里没有内存对齐的问题吧?
function BiAddSearch(PMyX: plongint; ff: double; Len: integer): integer; var Mid, StartPos, EndPos: integer; //search insert position by middle: ff insert to from startpos to endpos @SigNum
PX: Plongint;
//ffint:longint;//1
begin
StartPos := 0;
EndPos := Len - 1;
//ffint:=trunc(ff);////2
while (StartPos < EndPos - 1) do
begin
Mid := (EndPos - StartPos) div 2;
px := PMyx;
inc(px, mid + startpos);
if ff < px^ then EndPos := mid + startpos ////3
else StartPos := mid + startpos;
end; result
解决方案 »
- 关于返回值
- Tlistview选中的颜色,Viewstyle为vsReport,RowSelect为true,CheckBoxs为True,SmallImages的模式下
- 如何给字符串数组赋值?
- stringGrid的OnDrawCell的效率(高手请进)
- 一个关于数据库连接时出现的问题,在线等待,急!!!!!!!!!
- 求"TBarCode" 条码的控件!!! 收到后马上给分!!!!!
- query控件问题,在线等,急!!!!
- 以调用声音为主的几个问题
- 不使用AttachThreadInput如何获取任意程序光标位置
- 一个用delphi的图像处理难题, 大家救命阿
- ehlib4.2.16 picklist添加空值问题
- 关于qrchart
我过去也在bcb调试方式下统计过效率,发现效率比较一定要发布模式,要不不准确。
EndPos := mid + startpos
else
StartPos := mid + startpos;
应该是:if ff < px^ then
EndPos := mid + 1
else
StartPos := mid - 1;