
26-09-2006, 20:07
|
 |
|
|
חבר מתאריך: 02.10.05
הודעות: 2,355
|
|
|
Switch VS IF הכנתי לכם BanchMark
החלטתי לעשות BanchMark לבדוק מה יותר מהיר, Switch או תנאי IF, שניהם עם 3 שלבים, בתוך לולאה שרצה 100 פעמים בשביל שירגישו יותר את ההבדל, ואלו התוצאות! לקוד הזה:
קוד PHP:
<?php //this script is a tool devoloped by tNadav from www.fresh.co.il //banchMark tool $start_time = 0; $end_time = 0; //set start time $mtime = explode(" ", microtime()); $start_time = $mtime[1] + $mtime[0]; //start the code and we check the time its take... //start of code...
for($i = 1; $i < 100; $i++) { $d = $i; if($d > 3 ) $d = 1;
if($d == 1) echo "d is one <br />\n"; elseif($d == 2) echo "d is two <br />\n"; else echo "d is bigger then two <br />\n";
$d++; }
//end of code... //set end time $mtime = explode(" ", microtime()); $end_time = $mtime[1] + $mtime[0]; //set total time echo "\n<br />total time:"; echo substr(($end_time - $start_time), 0, 10); ?>
זה"כ זמן ריצה:
0.00020503
נגד הקוד הזה:
קוד PHP:
<?php <?php //this script is a tool devoloped by tNadav from www.fresh.co.il //banchMark tool $start_time = 0; $end_time = 0; //set start time $mtime = explode(" ", microtime()); $start_time = $mtime[1] + $mtime[0]; //start the code and we check the time its take... //start of code...
for($i = 1; $i < 100; $i++) { $d = $i; if($d > 3 ) $d = 1;
switch($d) { case 1: echo "d is one <br />\n"; break; case 2: echo "d is two <br />\n"; break; default: echo "d is bigger then two <br />\n"; } $d++; }
//end of code... //set end time $mtime = explode(" ", microtime()); $end_time = $mtime[1] + $mtime[0]; //set total time echo "\n<br />total time:"; echo substr(($end_time - $start_time), 0, 10); ?>
סה"כ זמן:
0.00018906
ועוד קוד.. שיטת עבודה שונה ב- Switch..
קוד PHP:
<?php //this script is a tool devoloped by tNadav from www.fresh.co.il //banchMark tool $start_time = 0; $end_time = 0; //set start time $mtime = explode(" ", microtime()); $start_time = $mtime[1] + $mtime[0]; //start the code and we check the time its take... //start of code...
for($i = 1; $i < 100; $i++) { $d = $i; if($d > 3 ) $d = 1;
switch(true) { case $d == 1: echo "d is one <br />\n"; break; case $d == 2: echo "d is two <br />\n"; break; default: echo "d is bigger then two <br />\n"; } $d++; }
//end of code... //set end time $mtime = explode(" ", microtime()); $end_time = $mtime[1] + $mtime[0]; //set total time echo "\n<br />total time:"; echo substr(($end_time - $start_time), 0, 10); ?>
סה"כ זמן:
0.00038719
מקווה לחוות דעת לגבי תנאים שווים וכו' וכו'..
_____________________________________
|