![](https://static.fresh.co.il/images/vBulletin/misc/blank.gif) |
![ישן](https://static.fresh.co.il/images/vBulletin/statusicon/post_old.gif)
26-08-2007, 02:22
|
|
|
חבר מתאריך: 25.08.03
הודעות: 9,114
|
|
isset באמת מהיר יותר מהפונקציה המקבילה לבדיקת איבר במערך, במיוחד מערך אסוציאטיבי (במערך רגיל הפער ממש ממש קלוש).
בכל אופן, זה לא ממש רלוונטי כשזה בעצם לא עושה את הפעולה הדרושה, כי אם איבר אחד יכיל את הערך null, ריצת הלולאה תופסק גם אם נותרו עשרות, מאות, אלפי או יותר איברים המכילים ערכים יותר רלוונטיים. (וגם null הוא ערך שיכול להיות מאד רלוונטי במקרים מסויימים).
מה שכן, יתכן ושימוש בשני הפונקציות יחד באמצעות OR כש-isset הוא הראשון, יהיה יעיל יותר.
אני מצרף קטע קוד לדוגמא:
קוד PHP:
<?
if(isset($arr['a']) || array_key_exists('a', $array))
{
// Some code
}
//...
while(isset($arr['a']) || array_key_exists('a', $array))
{
// Some code
}
?>
בנוסף, אני מצרף את קוד ה-php שהרצתי לצורך השוואת זמן הריצה בין isset ל-array_key_exists.
מדובר בקוד מעט ארוך, כיון שעשיתי בדיקות על מערכים בעלי סוג שונה, אורך שונה, בדיקת איבר בתחילת המערך, באמצע המערך, חורג בהרבה מתחום המערך וכדומה. (אני מניח שמבחינת הביצוע אין הבדל בין בדיקת איבר שתיאורטית חורג בהרבה מהיסט סוף המערך לבין איבר שתיאורטית נמצא בבית הבא שלאחר סיום המערך).
קוד PHP:
<?php
$total_time_start = array_sum(explode(' ',microtime()));
$times = 100000; // 1,000,000;
header('Content-Type: text/plain');
echo "\n\n--- Times: ".number_format($times)." ---\n\n\n";
$array = array(true);
echo "Array type: numeric\nCount: ".count($array)."\nKey: 0";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array[0]);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists(0, $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$array = array(true);
echo "\n\n------------------------\n\n";
echo "Array type: numeric\nCount: ".count($array)."\nKey: 1";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array[1]);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists(1, $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$array = array(true);
echo "\n\n------------------------\n\n";
echo "Array type: numeric\nCount: ".count($array)."\nKey: 1000";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array[1000]);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists(1000, $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
echo "\n\n------------------------\n\n";
echo "Array type: numeric\nCount: ".count($array)."\nKey: 0";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array[0]);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists(0, $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
echo "\n\n------------------------\n\n";
echo "Array type: numeric\nCount: ".count($array)."\nKey: 0";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array[15]);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists(15, $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
echo "\n\n------------------------\n\n";
echo "Array type: numeric\nCount: ".count($array)."\nKey: 1000";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array[1000]);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists(1000, $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$array = array('a'=>1);
echo "\n\n------------------------\n\n";
echo "Array type: associative\nCount: ".count($array)."\nKey: a";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array['a']);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists('a', $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$array = array('a'=>1);
echo "\n\n------------------------\n\n";
echo "Array type: associative\nCount: ".count($array)."\nKey: h";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array['h']);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists('h', $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$array = array(
'a'=>1,
'b'=>2,
'c'=>3,
'd'=>4,
'e'=>5,
'f'=>6,
'g'=>7,
'h'=>8,
'i'=>9,
'j'=>10,
'k'=>11,
'l'=>12,
'm'=>13,
'n'=>14,
'o'=>15,
'p'=>16,
'q'=>17,
'r'=>18,
's'=>19,
't'=>20);
echo "\n\n------------------------\n\n";
echo "Array type: associative\nCount: ".count($array)."\nKey: h";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array['h']);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists('h', $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$array = array(
'a'=>1,
'b'=>2,
'c'=>3,
'd'=>4,
'e'=>5,
'f'=>6,
'g'=>7,
'h'=>8,
'i'=>9,
'j'=>10,
'k'=>11,
'l'=>12,
'm'=>13,
'n'=>14,
'o'=>15,
'p'=>16,
'q'=>17,
'r'=>18,
's'=>19,
't'=>20);
echo "\n\n------------------------\n\n";
echo "Array type: associative\nCount: ".count($array)."\nKey: gh";
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
isset($array['gh']);
echo "\n\nFunction: isset\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
$time_start = array_sum(explode(' ',microtime()));
for($i=0; $i<$times; ++$i)
array_key_exists('gh', $array);
echo "\n\nFunction: array_key_exists\nTime: ".(array_sum(explode(' ',microtime())) - $time_start).' Sec';
echo "\n\n\nTotal: ".(array_sum(explode(' ',microtime())) - $total_time_start).' Sec';
?>
|
|