其中的function2()類似於wog_item_tool.php內的item_out()的內容(省略其他驗證的步驟等等)
函式1是利用串列的方式來處理陣列,函式2則是WOG中所用的處理方式
以下為兩個函式的程式碼
- 代碼: 選擇全部
function function_1($id,$num=1)
{
$item=unserialize('a:2:{i:1;i:2000;i:15;i:2;}');
if(!in_array($id,$item))
{
}
$item[$id]-=1;
$item=serialize($item);
return $item;
}
function function_2($id,$num=1)
{
$item=explode(',','1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,15,15');
$item2=array();
$chk=false;
for($i=0;$i < count($item);$i++)
{
if($item[$i] == $id && $chk==false)
{
$chk=true;
continue;
}
else
$item2[]=$item[$i];
}
if($chk == false)
{
}
return $item2;
}
運作速度比較 寫:以下為運作20次後的比較結果,列出5篇
- 代碼: 選擇全部
function 1 process time:0.00026599999999999 sec.
function 2 process time:0.001874 sec.
function 2 is Slower than function 1 over [color=red]-604.51127819554[/color] %
- 代碼: 選擇全部
function 1 process time:0.00026799999999999 sec.
function 2 process time:0.001859 sec.
function 2 is Slower than function 1 over [color=red]-593.65671641793[/color] %
- 代碼: 選擇全部
function 1 process time:0.00026500000000002 sec.
function 2 process time:0.001868 sec.
function 2 is Slower than function 1 over [color=red]-604.90566037733[/color] %
- 代碼: 選擇全部
function 1 process time:0.00026499999999996 sec.
function 2 process time:0.0018560000000001 sec.
function 2 is Slower than function 1 over [color=red]-600.3773584907[/color] %
- 代碼: 選擇全部
function 1 process time:0.000267 sec.
function 2 process time:0.001857 sec.
function 2 is Slower than function 1 over -595.50561797752 %
[point="簫哥"]<u>積分 +5</u>個人評論 寫:從上面的運作結果可以知道函式1遠比函式2要快得多,即使陣列只有兩個元素 函式1仍然比函式2快些
- 代碼: 選擇全部
function 1 process time:0.00026400000000004 sec.
function 2 process time:0.00031199999999998 sec.
function 2 is Slower than function 1 over -18.181818181791 %
其中,函式1的優勢在於不會隨著元素數量的增加而逐漸減慢速度
舉個例子來看
情況1:有2個ID:5跟3個ID:15,送入函式1與函式2後的運作結果如下(皆以重複運算20次為準)
- 代碼: 選擇全部
function 1 process time:0.00025500000000001 sec.
function 2 process time:0.000525 sec.
function 2 is Slower than function 1 over -105.88235294117 %
情況2:有5個ID:5與7個ID:15,再送入函式1與函式2後的運算結果如下
- 代碼: 選擇全部
function 1 process time:0.00025700000000001 sec.
function 2 process time:0.001071 sec.
function 2 is Slower than function 1 over -316.73151750971 %
從這兩個比較可以發現,在情況1與情況2下函式1的運算速度幾乎沒有影響
函式2則會隨著數量的增加而增加運算的時間,而運算時間與數量幾乎成正比
且若以函式一的方式來運作的話,即使內容包含二維以上陣列也很方便
我覺得改用這種處理方式的話可以讓玩家的裝備有不一樣的選擇,希望ET大可以考慮看看
涅魂做很棒程式碼!
at 2007-02-19 8:18 pm[/point]