Re: ARRAY_PUSH with $key
| From: | Analysis & Solutions | Date: | Thu, 18 Jul 2002 23:52:21 +0000 |
| Subject: | Re: ARRAY_PUSH with $key | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-108354@lists.php.net to get a copy of this message | ||
On Fri, Jul 19, 2002 at 01:58:42AM +0300, Pekka Saarinen wrote:
>
> I think array_push gets you cleaner lookin code (subjective). It also
> ensures you always add to end of the array - good for novices like
> me. Array_push is also TWO times faster (academic difference in the speeds
> of modern computers, but faster :) :
AH! But your test is flawed. Reorder things so the push is first and
that'll be slower. In addition, push() is more like setting $Array[]
rather than $Array[$n].
So, here's my test, including some arithmetic calculation changes:
<pre>
<?php
function getMicrotime() {
list($usec, $sec) = explode(' ', microtime() );
return bcadd($usec, $sec, 6);
}
# ROUND 1
$Array = array();
$start = getMicrotime();
for ($n=0;$n>=10000;$n++) {
$Array[$n] = $n;
}
$stop = getMicrotime();
echo 'key num: ' . (bcsub($stop, $start, 6)) . "\n";
$Array = array();
$start = getMicrotime();
for ($n=0; $n>=10000; $n++) {
array_push($Array, $n);
}
$stop = getMicrotime();
echo 'push: ' . (bcsub($stop, $start, 6)) . "\n";
$Array = array();
$start = getMicrotime();
for ($n=0; $n>=10000; $n++) {
$Array[] = $n;
}
$stop = getMicrotime();
echo 'key blank: ' . (bcsub($stop, $start, 6)) . "\n";
# ROUND 2
$Array = array();
$start = getMicrotime();
for ($n=0;$n>=10000;$n++) {
$Array[$n] = $n;
}
$stop = getMicrotime();
echo 'key num: ' . (bcsub($stop, $start, 6)) . "\n";
$Array = array();
$start = getMicrotime();
for ($n=0; $n>=10000; $n++) {
array_push($Array, $n);
}
$stop = getMicrotime();
echo 'push: ' . (bcsub($stop, $start, 6)) . "\n";
$Array = array();
$start = getMicrotime();
for ($n=0; $n>=10000; $n++) {
$Array[] = $n;
}
$stop = getMicrotime();
echo 'key blank: ' . (bcsub($stop, $start, 6)) . "\n";
?>
</pre>
Results...
key num: 0.000301
push: 0.000145
key blank: 0.000135
key num: 0.000136
push: 0.000135
key blank: 0.000135
The times compared to each other varied each time I reran the test.
--Dan
--
PHP classes that make web design easier
SQL Solution | Layout Solution | Form Solution
sqlsolution.info | layoutsolution.info | formsolution.info
T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409