Jump to content

Array Benchmarking


RobertP

Recommended Posts

results:

0.839771 << isset empty
2.519401 << array_key_exists empty
2.476911 << in_array empty

0.812198 << isset full
2.472904 << array_key_exists full
Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/test.php on line 58

 

code:

<?php

$iterations = 1000000;
$data = array();
$dataFull = array();

for ($i = 0; $i <= 1024; $i++) {
    $dataFull[$i] = str_repeat('1', 1024);
}

$startTime = microtime(true);
for ($i = 0; $i <= $iterations; $i++) {
    $foo = isset($data[$i]);
}
$totalTime = microtime(true) - $startTime;
echo number_format($totalTime, 6) . ' << isset empty<br />';

//====

$startTime = microtime(true);
for ($i = 0; $i <= $iterations; $i++) {
    $foo = array_key_exists($i, $data);
}
$totalTime = microtime(true) - $startTime;
echo number_format($totalTime, 6) . ' << array_key_exists empty<br />';

//====

$startTime = microtime(true);
for ($i = 0; $i <= $iterations; $i++) {
    $foo = in_array($i, $data);
}
$totalTime = microtime(true) - $startTime;
echo number_format($totalTime, 6) . ' << in_array empty<br />';

echo '<br />';

$startTime = microtime(true);
for ($i = 0; $i <= $iterations; $i++) {
    $foo = isset($dataFull[$i]);
}
$totalTime = microtime(true) - $startTime;
echo number_format($totalTime, 6) . ' << isset full<br />';

//====

$startTime = microtime(true);
for ($i = 0; $i <= $iterations; $i++) {
    $foo = array_key_exists($i, $dataFull);
}
$totalTime = microtime(true) - $startTime;
echo number_format($totalTime, 6) . ' << array_key_exists full<br />';

//====

$startTime = microtime(true);
for ($i = 0; $i <= $iterations; $i++) {
    $foo = in_array($i, $dataFull);
}
$totalTime = microtime(true) - $startTime;
echo number_format($totalTime, 6) . ' << in_array full<br />';

?>

Link to comment
Share on other sites

Did you have a point/question in this post?  Or just posting an FYI thing?

 

FYI,

1) isset is a language construct, array_key_exists is a function call.  Function calls have extra overhead, thus are slower

2) You're not using in_array properly.  in_array tests if a particular value exists in an array, not if a particular key exists

3) You should use isset(), except under one condition:  If you need to know if the key exists, but NULL is an acceptable value for that key (eg if it can contain say a DateTime object or null for none).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.