關於判斷式(IF)的內容與順序對程式執行速度之影響 |
尚未結案
|
Paicaso
中階會員 發表:140 回覆:124 積分:52 註冊:2002-09-04 發送簡訊給我 |
|
skurama
中階會員 發表:88 回覆:127 積分:73 註冊:2002-07-22 發送簡訊給我 |
以前學校的老師有提過: 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 發送簡訊給我 |
引述一段 help 中的關於 boolean evaluation 的控制給大家參考:
Type Switch Syntax {$B } or {$B-} {$BOOLEVAL ON} or {$BOOLEVAL OFF} Default {$B-} {$BOOLEVAL OFF} Scope LocalRemarks 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 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |