[分享]函式運作速度的比較

WOG設定安裝教學

版主: 涅魂, 簫哥


涅魂
 
文章: 4463
註冊時間: 2004-01-04 11:17 am
來自: Taiwan
性別: 男生

[分享]函式運作速度的比較

文章涅魂 » 2006-06-01 8:09 pm

閒閒沒事弄了這個來看看哪個函式運作的速度比較快,順便分享出來給大家做個參考 :o

以下這篇是參考item_out所做的比較(運算次數皆為100次)

Function 1
代碼: 選擇全部
function function_1($eq,$adds,$num)
{
   $temp=array();
   $d_id=array_unique($eq);
   $d_num=array_count_values($eq);
   if($d_num[$adds] >= $num)
   {
      $d_num[$adds]-=$num;
   }else
   {
      return "error equipment number.";
   }
   sort($d_id);
   for($b=0;$b < count($d_id);$b++)
   {
      $temp=array_pad($temp,count($temp)+$d_num[$d_id[$b]],$d_id[$b]);
   }
   return implode(",",$temp);
}


Function 2
代碼: 選擇全部
function function_2($item_now,$item_id,$item_num=0)
{
   $temp_pack2 = array();
   $adds=$item_id;
   $s="";
   for($j=0;$j<count($item_now);$j++)
   {
      if($adds[$i]!=$item_now[$j] || $item_num <= 0)
      {
         $temp_pack2[]=$item_now[$j];
      }else
      {
         $item_num--;
      }
   }
   if($item_num > 0)
   {
      return "error equipment number.";
   }
   return implode(",",$temp_pack2);
}


測試1 寫:Var
代碼: 選擇全部
$eq=explode(",","1,2,3,4,5,6,7,2,4,5,8,7,9,4,1,2,3,58,4,7,4,5,8,9,3,2,1,8");
$adds="4";
$num=30;

Result
代碼: 選擇全部
error equipment number.
error equipment number.
function 1 process time:0.003599 sec.
function 2 process time:0.016712 sec.
function 2 is faster than function 1 by -364.35 %

error equipment number.
error equipment number.
function 1 process time:0.004402 sec.
function 2 process time:0.015541 sec.
function 2 is faster than function 1 by -253.04 %

error equipment number.
error equipment number.
function 1 process time:0.0037470000000001 sec.
function 2 process time:0.016295 sec.
function 2 is faster than function 1 by -334.88 %


測試2 寫:var
代碼: 選擇全部
$eq=explode(",","1,2,3,4,5,6,7,2,4,5,8,7,9,4,1,2,3,58,4,7,4,5,8,9,3,2,1,8");
$adds="4";
$num=3;

Result
代碼: 選擇全部
1,1,1,2,2,2,2,3,3,3,4,4,5,5,5,6,7,7,7,8,8,8,9,9,58
1,2,3,5,6,7,2,5,8,7,9,1,2,3,58,4,7,4,5,8,9,3,2,1,8
function 1 process time:0.018574 sec.
function 2 process time:0.017322 sec.
function 2 is faster than function 1 by 6.74 %

1,1,1,2,2,2,2,3,3,3,4,4,5,5,5,6,7,7,7,8,8,8,9,9,58
1,2,3,5,6,7,2,5,8,7,9,1,2,3,58,4,7,4,5,8,9,3,2,1,8
function 1 process time:0.017874 sec.
function 2 process time:0.017449 sec.
function 2 is faster than function 1 by 2.38 %

1,1,1,2,2,2,2,3,3,3,4,4,5,5,5,6,7,7,7,8,8,8,9,9,58
1,2,3,5,6,7,2,5,8,7,9,1,2,3,58,4,7,4,5,8,9,3,2,1,8
function 1 process time:0.017349 sec.
function 2 process time:0.017461 sec.
function 2 is faster than function 1 by -0.65 %


以上僅供參考… :o




ETERNAL
 
文章: 2937
註冊時間: 2003-12-03 11:08 pm
性別: 男生

文章ETERNAL » 2006-06-02 9:24 am

看起來Function 1 好像比較快

Function 1 跑出來的結果正確嗎
不知道V3能不能改成這樣

不過Function 1 用的php函數比較多,反而比較快
真奇怪...


水色論壇 http://www.et99.net
簡恩峻分享


涅魂
 
文章: 4463
註冊時間: 2004-01-04 11:17 am
來自: Taiwan
性別: 男生

文章涅魂 » 2006-06-02 8:22 pm

我覺得function_1()的優勢在於處理較少資料時的速度以及除錯的速度比較快

而function_2()則比較適合用來處理較長串的資料

下面再提供兩組加大$eq後的測試數據,測試之後兩個函數的執行時間差距開始拉大

test1 寫:var
代碼: 選擇全部
$eq=explode(",","1,2,3,4,5,6,7,2,4,5,8,7,9,4,1,2,3,58,4,7,4,5,8,9,3,2,1,8,8,7,4,6,50,10,889,1578,15,40404,403,4,8,4654,605,457");
$adds="4";
$num=3;

Result
代碼: 選擇全部
function 1 process time:0.043764 sec.
function 2 process time:0.02805 sec.
function 2 is faster than function 1 by [color=blue]35.91 %[/color]

function 1 process time:0.043474 sec.
function 2 process time:0.027333 sec.
function 2 is faster than function 1 by [color=blue]37.13 %[/color]

function 1 process time:0.046199 sec.
function 2 process time:0.029058 sec.
function 2 is faster than function 1 by [color=blue]37.1 %[/color]


test2 寫:Var
代碼: 選擇全部
$eq=explode(",","1,2,3,4,5,6,7,2,4,5,8,7,9,4,1,2,3,58,4,7,4,5,8,9,3,2,1,8,8,7,4,6,50,10,889,1578,15,40404,403,4,8,4654,605,457,589,44,1654,3214878,421,46465,87,354");
$adds="4";
$num=3;

Result
代碼: 選擇全部
function 1 process time:0.064079 sec.
function 2 process time:0.036194 sec.
function 2 is faster than function 1 by [color=blue]43.52 %[/color]

function 1 process time:0.063669 sec.
function 2 process time:0.036043 sec.
function 2 is faster than function 1 by [color=blue]43.39 %[/color]

function 1 process time:0.068967 sec.
function 2 process time:0.034045 sec.
function 2 is faster than function 1 by [color=blue]50.64 %[/color]


而我在另外測試其他組數據後也得到了較為確定的結果

function_1()的優勢在於處理較多重複物品時能展現他運作的優勢

第一篇中正常運作的情況下雖然僅比function_2()快上2~7%的速度

但若增加$eq中重複的物品後,運作結果function_1()可以比Function2()快上80~96%($eq如下所示)
代碼: 選擇全部
$eq=explode(",","1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,4,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5");


反之,function_2()則是在處理許多不同物品時較能發揮他的優勢

而且跟function_1()相比較的話,只要在沒有錯誤的情況下$eq中不同類物品越多則function_2()的運作速度可以比function_1()還快上30~100% ($eq如下所示)
代碼: 選擇全部
$eq=range(1,50);

所以說,以速度考量的話function_2()是最佳選擇(也就是目前WOG中的運作類型)

而若是程式經常被破壞或道具類型少而數量相當大時用function_1()可以有效減少流量

以上,我看下次來貼其他的數據來討論好了 :o




回到 精華區

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 2 位訪客