如何寫 比 Trim 還快的函數? |
答題得分者是:change.jian
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
|
change.jian
版主 發表:29 回覆:620 積分:439 註冊:2003-06-02 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
change.jian 版主,您好! sorry, 最近比較忙,所以較少上來看看。
關於 Trim 函數,原型如下。
function Trim(const S: string): string; var I, L: Integer; begin L := Length(S); I := 1; while (I <= L) and (S[I] <= ' ') do Inc(I); if I > L then Result := '' else begin while S[L] <= ' ' do Dec(L); Result := Copy(S, I, L - I 1); end; end;老實說,我對於這種一個個字元迴圈判斷的作法有點微詞。 因此對它的效率,產生疑問?? 由於目前我必須使用大量的 Trim 函數。 因此,對於該函數效能的提升,也是當務之急。 之前,不知道在哪個網站看過。 是利用Delphi assembler 更改 Sysutils 一些函數。 可惜,我忘了..... 我的想法是既然 While S[I]=.... 去判斷。 就如同 Pos 是用asm寫的會比 迴圈判斷來的快。 可惜,小弟我對 assembler 不是很熟悉。 也請各位幫幫忙吧! |
poemkevin
初階會員 發表:26 回覆:77 積分:30 註冊:2002-10-19 發送簡訊給我 |
|
change.jian
版主 發表:29 回覆:620 積分:439 註冊:2003-06-02 發送簡訊給我 |
to wameng大大:
1.關於Trim函數,我之前己有看過原source code.寫組合語言對我來說,已是十年前的事了.那些暫存器,早忘光了.我只是在想,source code裡,也是迴圈加if,只有最後一行才是copy.而如果以我的想法,即便以組合語言寫,前面的迴圈應該也是轉成若干的JE與JNE指令吧. 2.前幾天去參加Delphi 2005發表會,其中指到新的Delphi指令叫inline(不知wameng是否有參加).依據李維先生的說法,如果有一段迴圈大量呼叫一個fuction或procedure時,可以把該function加上個inline的指令,新的compiler會把該function內的code直接塞入呼叫的迴圈內.利用這樣的做法,可以減少呼叫function時,會產生堆疊等的指令操作,依李維先生的說法,這樣可以增加20~30%的速度.我才在想,如果直接把Trim的source code寫入呼叫的迴圈內,應該也可以達到與inline同樣的效果.最少,在還沒有找到真的有效率的trim source code前,這應該可以減少一些指令碼吧
引言: change.jian 版主,您好! sorry, 最近比較忙,所以較少上來看看。 關於 Trim 函數,原型如下。function Trim(const S: string): string; var I, L: Integer; begin L := Length(S); I := 1; while (I <= L) and (S[I] <= ' ') do Inc(I); if I > L then Result := '' else begin while S[L] <= ' ' do Dec(L); Result := Copy(S, I, L - I 1); end; end;老實說,我對於這種一個個字元迴圈判斷的作法有點微詞。 因此對它的效率,產生疑問?? 由於目前我必須使用大量的 Trim 函數。 因此,對於該函數效能的提升,也是當務之急。 之前,不知道在哪個網站看過。 是利用Delphi assembler 更改 Sysutils 一些函數。 可惜,我忘了..... 我的想法是既然 While S[I]=.... 去判斷。 就如同 Pos 是用asm寫的會比 迴圈判斷來的快。 可惜,小弟我對 assembler 不是很熟悉。 也請各位幫幫忙吧! |
poemkevin
初階會員 發表:26 回覆:77 積分:30 註冊:2002-10-19 發送簡訊給我 |
change.jian 大大:
您所說的inline請問要怎麼用在迴圈上啊!
我去看delphi help,
只看到這樣
Inline assembler code
The built-in assembler allows you to write assembler code within Object Pascal programs. It has the following features: Allows for inline assembly
Supports all instructions found in the Intel Pentium III, SIMD, and the AMD Athlon (including 3D Now!)
Provides no macro support, but allows for pure assembler function procedures
Permits the use of Object Pascal identifiers, such as constants, types, and variables in assembler statements As an alternative to the built-in assembler, you can link to object files that contain external procedures and functions. See External declarations for more information. Note: If you have external assembler code that you want to use in your applications, you should consider rewriting it in Object Pascal or minimally reimplement it using the inline assembler. 好像也只是用在組合語言的程式啊! ===========================
沉思, 再沉思! 學習, 再學習!
生活隨喜, 簡單自在^^
===========================
|
change.jian
版主 發表:29 回覆:620 積分:439 註冊:2003-06-02 發送簡訊給我 |
to poemkevin:
我說的inline,是Delphi 2005的新指令,用於function/procedure的宣告後面,並非指用於組合語言
引言: change.jian 大大: 您所說的inline請問要怎麼用在迴圈上啊! 我去看delphi help, 只看到這樣 Inline assembler code The built-in assembler allows you to write assembler code within Object Pascal programs. It has the following features: Allows for inline assembly Supports all instructions found in the Intel Pentium III, SIMD, and the AMD Athlon (including 3D Now!) Provides no macro support, but allows for pure assembler function procedures Permits the use of Object Pascal identifiers, such as constants, types, and variables in assembler statements As an alternative to the built-in assembler, you can link to object files that contain external procedures and functions. See External declarations for more information. Note: If you have external assembler code that you want to use in your applications, you should consider rewriting it in Object Pascal or minimally reimplement it using the inline assembler. 好像也只是用在組合語言的程式啊! =========================== 沉思, 再沉思! 學習, 再學習! 生活隨喜, 簡單自在^^ =========================== |
poemkevin
初階會員 發表:26 回覆:77 積分:30 註冊:2002-10-19 發送簡訊給我 |
Dear change.jian 大大:
謝謝您的指導,原來這是delphi2005才能有這功能喔!!
不知是否可以將inline的功能移植到delphi6,
不然, 為了新功能, 豈非不是每年都得花上好多銀子向borland公司朝貢^^"
這樣會很痛苦的... 剛在網路看到了一篇關於trim的bug, 嘿...去除空白的問題還真多嘿=.=
轉貼如下:
引言: 我一直以為Trim函數的功能是把字串的頭尾"空格"去掉,但最近程序出了個非常怪的bug,最後好不容易才發現是對Trim函數的誤解,看看它的原碼:=========================== 沉思, 再沉思! 學習, 再學習! 生活隨喜, 簡單自在^^ ===========================function Trim(const S: string): string; var I, L: Integer; begin L := Length(S); I := 1; while (I < = L) and (S[I] < = ' ') do Inc(I); if I > L then Result := '' else begin while S[L] < = ' ' do Dec(L); Result := Copy(S, I, L - I 1); end; end;看到了嗎?它用了"< =",就是說不是光去掉"空格",小於"空格"的ASCII碼都去掉了! 我原來的資料裡末尾有回車/換行(#13#10)符,結果也被去掉了,因此引起了奇怪的bug |
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |