線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2519
推到 Plurk!
推到 Facebook!

如何寫 比 Trim 還快的函數?

答題得分者是:change.jian
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-10 15:49:09 IP:61.31.xxx.xxx 未訂閱
討論 Trim 函數,如何用 Delphi asm 代替。 發表人 - wameng 於 2004/11/10 15:59:45
change.jian
版主


發表:29
回覆:620
積分:439
註冊:2003-06-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-10 16:29:51 IP:61.218.xxx.xxx 未訂閱
to wameng大大: 小弟看了一下,覺得Delphi本身的Trim函數,應該已經很有效率了.因為從頭到尾都在比對字元,只有最後才呼叫一次的copy.以Delphi目前的編譯技術,我想可以再改進的空間應該有限. 如果wameng大大是因為需要在迴圈內大量呼叫Trim的話,那麼是否可以換個方式,把Trim裡的code直接搬到迴圈裡,這樣可以滅少大量堆疊及回傳等動作,應該可以提升個20~30%吧. 以上是小弟的想法,若有錯誤還請糾正.
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-15 10:41:23 IP:61.222.xxx.xxx 未訂閱
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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-11-15 17:06:32 IP:210.202.xxx.xxx 未訂閱
http://fundementals.sourceforge.net/cStrings.html http://www.angelfire.com/hi5/delphizeus/smallutil.html 這都用一些asm編寫函數 只可惜小弟也找不到代替trim這函數的asm寫法 發表人 - poemkevin 於 2004/11/15 17:11:37
change.jian
版主


發表:29
回覆:620
積分:439
註冊:2003-06-02

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-11-15 23:20:02 IP:218.169.xxx.xxx 未訂閱
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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-11-16 23:00:31 IP:61.221.xxx.xxx 未訂閱
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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-11-17 08:51:26 IP:61.218.xxx.xxx 未訂閱
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

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-11-17 11:14:16 IP:210.202.xxx.xxx 未訂閱
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

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-11-17 14:59:01 IP:61.222.xxx.xxx 未訂閱
看來這一題,是沒完沒了了。.....    to change.jian 大大    >Delphi 2005發表會,我沒參加。 不過,也有些網站對這個技術有些討論。大約懂些! 唉~ 人老了,對新的技術也不是很積極了。 還在用 Delphi 5 呢。懶得換了! 夠用就好!.......   不過
系統時間:2024-07-05 10:54:19
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!