全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1738
推到 Plurk!
推到 Facebook!

關於判斷式(IF)的內容與順序對程式執行速度之影響

尚未結案
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-10-12 16:59:08 IP:61.59.xxx.xxx 未訂閱
關於判斷式(IF)的內容與順序對程式執行速度之影響 例如 IF (***) AND (***) Then 或 IF (***) Then Begin IF (***) Then ...... 對程式執行速度是否有差別影響
skurama
中階會員


發表:88
回覆:127
積分:73
註冊:2002-07-22

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-10-13 10:50:34 IP:61.219.xxx.xxx 未訂閱
以前學校的老師有提過: IF (A) and (B) then begin end; 如果條件A為 False,則不會再判斷B, 為了提高效能, 通常會把容易為false(或比較關鍵的判斷)的條件放在A來判斷 如果是 If (A) then begin end; if (B) then begin end; 如果A為true,B也為true時,則兩個判斷都會執行, 但是A為false時,B也會再判斷一次。 所以通常會選 IF (A) and (B) then begin .... end;, 小弟愚見,參考參考。 ---------------- 昂首千丘遠, 嘯傲風間, 堪尋敵手共論劍, 高處不甚寒! ----------------
------
----------------
初出芧房程設師,
左鍵右鼠寫程式,
日扣夜寫眼框溼,
望能早成系分師。
----------------
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-10-13 11:49:50 IP:203.95.xxx.xxx 未訂閱
引述一段 help 中的關於 boolean evaluation 的控制給大家參考:
Type      Switch
Syntax    {$B } or {$B-}
          {$BOOLEVAL ON} or {$BOOLEVAL OFF}
Default   {$B-}
          {$BOOLEVAL OFF}
Scope     Local
Remarks The $B directive switches between the two different models of Delphi code generation for the and and or Boolean operators. In the {$B } state, the compiler generates code for complete Boolean expression evaluation. This means that every operand of a Boolean expression built from the and and or operators is guaranteed to be evaluated, even when the result of the entire expression is already known. In the {$B-} state, the compiler generates code for short-circuit Boolean expression evaluation, which means that evaluation stops as soon as the result of the entire expression becomes evident in left to right order of evaluation. 所以系統預設是會以 {$B-} 的方式來 evaluation boolean 的. 也就是skurama兄的建議!
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-10-14 12:57:53 IP:61.59.xxx.xxx 未訂閱
謝謝二位先進的說明 然在程式中位增進許多功能,其不免會增加許多IF之判斷式, 對於速度之影響應該也是在所難免 如果比較多層之判斷,在合理邏輯下應該用Case of會更快對吧? 另針對IF方面之條件為Boolean or Integer or String,對於不同類型之屬性是否又有差異?當然只有一小段之判斷可能察覺不出來,但如果是一大串應該還是會有差別?
系統時間:2024-04-29 23:36:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!