来自:http://bbs.csdn.net/topics/330048800

---------------------------------------------------------

我们经常在DELPHI中用const来定义常量,用const来保护函数参数,其实在用const保护函数参数还有另一个更为重要的作用,提高应用程序的执行效率,尤其是在多线程多核下效果更明显。原因是:普通的函数参数如Add(AValue: string),编译器在传入参数的时候先把变量复制一份,然后当成AValue传入Add,函数结束的时候进行销毁,你在参数上加了const,编译器在传入参数的时候不会进行复制,而是直接传地址,并在编译期间检查不能修改AValue值,我们知道DELPHI的内存管理在申请内存的时候是会加锁的,因此如果调用函数频繁,而且没有加const,这样会造成线程排队等候,性能会不如单线程,const只是对string、结构体等非基本类型有提高效率的作用,对Integer等基本类型(栈变量)不起作用。

1、const的类型检查,以下代码可以修改const参数的值

  1. procedure TFmMain.EditConstParameter(const ARecordTest: TRecordTest);
  2. var
  3. pPoint: PRecordTest;
  4. begin
  5. pPoint := @ARecordTest;
  6. pPoint.A := ;
  7. ShowMessage(IntToStr(ARecordTest.A));
  8. end;
  9.  
  10. procedure TFmMain.btnEditConstClick(Sender: TObject);
  11. var
  12. ARecordTest: TRecordTest;
  13. begin
  14. ARecordTest.A := ;
  15. EditConstParameter(ARecordTest);
  16. Inc(ARecordTest.A);
  17. ShowMessage(IntToStr(ARecordTest.A));
  18. end;

2、const提高代码性能,使用const提高代码性能,大家可以把以下例子在自己电脑上测试。

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7. Dialogs, StdCtrls, DateUtils;
  8.  
  9. const
  10. WM_Complete = WM_USER + ;
  11. type
  12. TRecordTest = record
  13. A: Integer;
  14. B: Integer;
  15. C: Integer;
  16. D: Integer;
  17. E: Integer;
  18. F: Integer;
  19. AStr: string;
  20. BStr: string;
  21. CStr: string;
  22. DStr: string;
  23. EStr: string;
  24. FStr: string;
  25. FCommit: array[..**] of Char;
  26. end;
  27. PRecordTest = ^TRecordTest;
  28.  
  29. TTestThread = class;
  30.  
  31. TFmMain = class(TForm)
  32. grpConst: TGroupBox;
  33. cbbConstThreadNum: TComboBox;
  34. lblThreadConst: TLabel;
  35. btnConstStart: TButton;
  36. btnConstStop: TButton;
  37. grp1: TGroupBox;
  38. lbl1: TLabel;
  39. cbbUnConstThreadNum: TComboBox;
  40. btnUnConstStart: TButton;
  41. btnUnConstStop: TButton;
  42. mmoText: TMemo;
  43. btnEditConst: TButton;
  44. procedure btnConstStartClick(Sender: TObject);
  45. procedure btnConstStopClick(Sender: TObject);
  46. procedure btnUnConstStartClick(Sender: TObject);
  47. procedure btnUnConstStopClick(Sender: TObject);
  48. procedure btnEditConstClick(Sender: TObject);
  49. private
  50. { Private declarations }
  51. FStartTime, FEndTime: TDateTime;
  52. FConstThread, FUnConstThread: array of TTestThread;
  53. protected
  54. procedure WMComplete(var Msg: TMessage); message WM_Complete;
  55. public
  56. {* 修改const函数变量 *}
  57. procedure EditConstParameter(const ARecordTest: TRecordTest);
  58. {* 线程测试函数 *}
  59. function ConstTestA(const ARecordTest: TRecordTest): Integer;
  60. function ConstTestB(const ARecordTest: TRecordTest): Integer;
  61. function ConstTestC(const ARecordTest: TRecordTest): Integer;
  62. function ConstTestD(const ARecordTest: TRecordTest): Integer;
  63. function ConstTestE(const ARecordTest: TRecordTest): Integer;
  64. function ConstTestF(const ARecordTest: TRecordTest): Integer;
  65. function UnConstTestA(ARecordTest: TRecordTest): Integer;
  66. function UnConstTestB(ARecordTest: TRecordTest): Integer;
  67. function UnConstTestC(ARecordTest: TRecordTest): Integer;
  68. function UnConstTestD(ARecordTest: TRecordTest): Integer;
  69. function UnConstTestE(ARecordTest: TRecordTest): Integer;
  70. function UnConstTestF(ARecordTest: TRecordTest): Integer;
  71. end;
  72.  
  73. TTestThread = class(TThread)
  74. private
  75. FConst: Boolean;
  76. protected
  77. procedure Execute; override;
  78. end;
  79.  
  80. var
  81. FmMain: TFmMain;
  82.  
  83. implementation
  84.  
  85. {$R *.dfm}
  86.  
  87. { TFmMain }
  88.  
  89. procedure TFmMain.EditConstParameter(const ARecordTest: TRecordTest);
  90. var
  91. pPoint: PRecordTest;
  92. begin
  93. pPoint := @ARecordTest;
  94. pPoint.A := ;
  95. ShowMessage(IntToStr(ARecordTest.A));
  96. end;
  97.  
  98. procedure TFmMain.btnEditConstClick(Sender: TObject);
  99. var
  100. ARecordTest: TRecordTest;
  101. begin
  102. ARecordTest.A := ;
  103. EditConstParameter(ARecordTest);
  104. Inc(ARecordTest.A);
  105. ShowMessage(IntToStr(ARecordTest.A));
  106. end;
  107.  
  108. function TFmMain.ConstTestA(const ARecordTest: TRecordTest): Integer;
  109. var
  110. i, j: Integer;
  111. begin
  112. j := ARecordTest.A;
  113. for i := to do
  114. begin
  115. j := j + ;
  116. end;
  117. Result := j;
  118. ConstTestB(ARecordTest);
  119. end;
  120.  
  121. function TFmMain.ConstTestB(const ARecordTest: TRecordTest): Integer;
  122. var
  123. i, j: Integer;
  124. begin
  125. j := ARecordTest.A;
  126. for i := to do
  127. begin
  128. j := j + ;
  129. end;
  130. Result := j;
  131. ConstTestC(ARecordTest);
  132. end;
  133.  
  134. function TFmMain.ConstTestC(const ARecordTest: TRecordTest): Integer;
  135. var
  136. i, j: Integer;
  137. begin
  138. j := ARecordTest.A;
  139. for i := to do
  140. begin
  141. j := j + ;
  142. end;
  143. Result := j;
  144. ConstTestD(ARecordTest);
  145. end;
  146.  
  147. function TFmMain.ConstTestD(const ARecordTest: TRecordTest): Integer;
  148. var
  149. i, j: Integer;
  150. begin
  151. j := ARecordTest.A;
  152. for i := to do
  153. begin
  154. j := j + ;
  155. end;
  156. Result := j;
  157. ConstTestE(ARecordTest);
  158. end;
  159.  
  160. function TFmMain.ConstTestE(const ARecordTest: TRecordTest): Integer;
  161. var
  162. i, j: Integer;
  163. begin
  164. j := ARecordTest.A;
  165. for i := to do
  166. begin
  167. j := j + ;
  168. end;
  169. Result := j;
  170. ConstTestF(ARecordTest);
  171. end;
  172.  
  173. function TFmMain.ConstTestF(const ARecordTest: TRecordTest): Integer;
  174. var
  175. i, j: Integer;
  176. begin
  177. j := ARecordTest.A;
  178. for i := to do
  179. begin
  180. j := j + ;
  181. end;
  182. Result := j;
  183. end;
  184.  
  185. function TFmMain.UnConstTestA(ARecordTest: TRecordTest): Integer;
  186. var
  187. i, j: Integer;
  188. begin
  189. j := ARecordTest.A;
  190. for i := to do
  191. begin
  192. j := j + ;
  193. end;
  194. Result := j;
  195. UnConstTestB(ARecordTest);
  196. end;
  197.  
  198. function TFmMain.UnConstTestB(ARecordTest: TRecordTest): Integer;
  199. var
  200. i, j: Integer;
  201. begin
  202. j := ARecordTest.A;
  203. for i := to do
  204. begin
  205. j := j + ;
  206. end;
  207. Result := j;
  208. UnConstTestC(ARecordTest);
  209. end;
  210.  
  211. function TFmMain.UnConstTestC(ARecordTest: TRecordTest): Integer;
  212. var
  213. i, j: Integer;
  214. begin
  215. j := ARecordTest.A;
  216. for i := to do
  217. begin
  218. j := j + ;
  219. end;
  220. Result := j;
  221. UnConstTestD(ARecordTest);
  222. end;
  223.  
  224. function TFmMain.UnConstTestD(ARecordTest: TRecordTest): Integer;
  225. var
  226. i, j: Integer;
  227. begin
  228. j := ARecordTest.A;
  229. for i := to do
  230. begin
  231. j := j + ;
  232. end;
  233. Result := j;
  234. UnConstTestE(ARecordTest);
  235. end;
  236.  
  237. function TFmMain.UnConstTestE(ARecordTest: TRecordTest): Integer;
  238. var
  239. i, j: Integer;
  240. begin
  241. j := ARecordTest.A;
  242. for i := to do
  243. begin
  244. j := j + ;
  245. end;
  246. Result := j;
  247. UnConstTestF(ARecordTest);
  248. end;
  249.  
  250. function TFmMain.UnConstTestF(ARecordTest: TRecordTest): Integer;
  251. var
  252. i, j: Integer;
  253. begin
  254. j := ARecordTest.A;
  255. for i := to do
  256. begin
  257. j := j + ;
  258. end;
  259. Result := j;
  260. end;
  261.  
  262. procedure TFmMain.WMComplete(var Msg: TMessage);
  263. begin
  264. FEndTime := Now;
  265. mmoText.Lines.Add('Spend Time: ' + IntToStr(MilliSecondsBetween(FStartTime, FEndTime)));
  266. end;
  267.  
  268. { TTestThread }
  269.  
  270. procedure TTestThread.Execute;
  271. var
  272. ARecordTest: TRecordTest;
  273. begin
  274. inherited;
  275. ARecordTest.A := ;
  276. while ARecordTest.A < do
  277. begin
  278. if FConst then
  279. begin
  280. Inc(ARecordTest.A);
  281. FmMain.ConstTestA(ARecordTest);
  282. end
  283. else
  284. begin
  285. Inc(ARecordTest.A);
  286. FmMain.UnConstTestA(ARecordTest);
  287. end;
  288. end;
  289. SendMessage(FmMain.Handle, WM_Complete, , );
  290. end;
  291.  
  292. procedure TFmMain.btnConstStartClick(Sender: TObject);
  293. var
  294. i: Integer;
  295. begin
  296. FStartTime := Now;
  297. SetLength(FConstThread, StrToInt(cbbConstThreadNum.Text));
  298. for i := Low(FConstThread) to High(FConstThread) do
  299. begin
  300. FConstThread[i] := TTestThread.Create(True);
  301. FConstThread[i].FreeOnTerminate := True;
  302. FConstThread[i].FConst := True;
  303. end;
  304. for i := Low(FConstThread) to High(FConstThread) do
  305. begin
  306. FConstThread[i].Resume;
  307. end;
  308. btnConstStart.Enabled := False;
  309. btnConstStop.Enabled := True;
  310. end;
  311.  
  312. procedure TFmMain.btnConstStopClick(Sender: TObject);
  313. var
  314. i: Integer;
  315. begin
  316. if Length(FConstThread) = then Exit;
  317. for i := Low(FConstThread) to High(FConstThread) do
  318. begin
  319. FConstThread[i].Terminate;
  320. end;
  321. SetLength(FConstThread, );
  322. btnConstStart.Enabled := True;
  323. btnConstStop.Enabled := False;
  324. end;
  325.  
  326. procedure TFmMain.btnUnConstStartClick(Sender: TObject);
  327. var
  328. i: Integer;
  329. begin
  330. FStartTime := Now;
  331. SetLength(FUnConstThread, StrToInt(cbbUnConstThreadNum.Text));
  332. for i := Low(FUnConstThread) to High(FUnConstThread) do
  333. begin
  334. FUnConstThread[i] := TTestThread.Create(True);
  335. FUnConstThread[i].FreeOnTerminate := True;
  336. FUnConstThread[i].FConst := False;
  337. end;
  338. for i := Low(FUnConstThread) to High(FUnConstThread) do
  339. begin
  340. FUnConstThread[i].Resume;
  341. end;
  342. btnUnConstStart.Enabled := False;
  343. btnUnConstStop.Enabled := True;
  344. end;
  345.  
  346. procedure TFmMain.btnUnConstStopClick(Sender: TObject);
  347. var
  348. i: Integer;
  349. begin
  350. if Length(FUnConstThread) = then Exit;
  351. for i := Low(FUnConstThread) to High(FUnConstThread) do
  352. begin
  353. FUnConstThread[i].Terminate;
  354. end;
  355. SetLength(FUnConstThread, );
  356. btnUnConstStart.Enabled := True;
  357. btnUnConstStop.Enabled := False;
  358. end;
  359.  
  360. end.

DELPHI用const来提高应用程序在多核多线程下的性能的更多相关文章

  1. Linux的虚拟内存管理-如何分配和释放内存,以提高服务器在高并发情况下的性能,从而降低了系统的负载

    Linux的虚拟内存管理有几个关键概念: Linux 虚拟地址空间如何分布?malloc和free是如何分配和释放内存?如何查看堆内内存的碎片情况?既然堆内内存brk和sbrk不能直接释放,为什么不全 ...

  2. 使用异步 I/O 大大提高应用程序的性能

    使用异步 I/O 大大提高应用程序的性能 学习何时以及如何使用 POSIX AIO API Linux® 中最常用的输入/输出(I/O)模型是同步 I/O.在这个模型中,当请求发出之后,应用程序就会阻 ...

  3. delphi 一个自动控制机的硅控板检测程序,用多线程和API,没有用控件,少做改动就能用 用485开发

    一个自动控制机的硅控板检测程序,用多线程和API,没有用控件,少做改动就能用Unit CommThread; Interface Uses  Windows, Classes, SysUtils, G ...

  4. (转)对《30个提高Web程序执行效率的好经验》的理解

    阅读了博客园发布的IT文章<30个提高Web程序执行效率的好经验>,这30条准则对我们web开发是非常有用的,不过大家可能对其中的一些准则是知其然而不知其所以然. 下面是我对这些准则的理解 ...

  5. 提高WPF程序性能的几条建议

    这篇博客将介绍一些提高WPF程序的建议(水平有限,如果建议有误,请指正.) 1. 加快WPF程序的启动速度: (1).减少需要显示的元素数量,去除不需要或者冗余的XAML元素代码. (2).使用UI虚 ...

  6. Delphi XE5教程3:实例程序

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  7. 解读30个提高Web程序执行效率的好经验

    其实微博是个好东西,关注一些技术博主之后,你不用再逛好多论坛了,因为一些很好的文章微博会告诉你,最近看到酷勤网推荐的一篇文章<30个提高Web程序执行效率的好经验>,文章写得不错,提到一些 ...

  8. 一个用于每一天JavaScript示例-使用缓存计算(memoization)为了提高应用程序性能

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 【翻译】七个习惯提高Python程序的性能

    原文链接:https://www.tutorialdocs.com/article/7-habits-to-improve-python-programs.html 掌握一些技巧,可尽量提高Pytho ...

随机推荐

  1. BZOJ 3531 SDOI2014 旅行 树链剖分+线段树动态开点

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3531 题意概述: 给出一棵N个点的树,树上的每个结点有一个颜色和权值,支持以下四种操作: ...

  2. 程序运行bug查看

    1.左击计算机进入管理,点击windows日志,查看程序信息. 可以方便看到报错信息.

  3. 段寻址*****************************TBD

    fffff880`01b05be1 ff9708020000    call    qword ptr [rdi+208h] ds:002b:fffff980`0554ae88=fffffa8004b ...

  4. golang and intellij

    有一个项目,混合了java和go,需要在intellij中安装go的插件. OK,网上的信息简直混乱不堪,两个流派,一个流派就是装插件,一个流派就是编译插件,各种折腾,还是安装不了,谁知柳暗花明又一村 ...

  5. 子查询 做where条件 做 from的临时表 ,做select的一个字段 等

    子查询 做where条件 做 from的临时表 ,做select的一个字段 等

  6. 算法学习——st表

    st表是一种基于倍增思想的DP. 用于求一个数列中的某个区间的最大/最小值. 用st[i][j]表示从第i个开始往后2^j个点,最大的是多少. 我们令k[i]表示2^i等于多少 那么有转移方程 st[ ...

  7. Small things are better

    Yesterday I had fun time repairing 1.5Tb ext3 partition, containing many millions of files. Of cours ...

  8. [模拟赛] GotoAndPlay

    GotoAndPlay 10月3日,在杭州市西湖景区,一只小松鼠不停地接受一道道食物,花生. 玉米.饼干,可谓来者不拒,憨态可掬的模样吸引了众多围观者... Description 小松鼠终于吃撑了, ...

  9. nodejs npm insttall 带不带-g这个参数的区别

    -g 中的g是global的意思所以带-g这个参数是全局安装,不带-g这个参数是本地安装. 在windows系统中全局安装的目录在:C:\Users\linsenq\AppData\Roaming\n ...

  10. (转)HTTP请求中URL地址的编码和解码

    HTTP请求中,类似   http%3A%2F%2Fwww.baidu.com%2Fcache%2Fuser%2Fhtml%2Fv3Jump.html  的地址 如何解码成    http://www ...