SplFastArray

Few days ago I’ve downloaded last version of php5.3 from snaps.php.net and I’ve made some tests with SplFastArray. Results are very strange for me:

here is result of last test I’ve made few minutes ago.

$ php fastarray.php
——————————————————————-
marker time index ex time perct
——————————————————————-
Start 1213124261.36967200 – 0.00%
——————————————————————-
simple array foreach 1213124261.57657300 0.20690083503723 13.10%
——————————————————————-
fast array foreach 1213124261.89191900 0.31534600257874 19.96%
——————————————————————-
simple array for 1213124262.36776400 0.47584509849548 30.12%
——————————————————————-
fast array for 1213124262.94954500 0.58178091049194 36.82%
——————————————————————-
Stop 1213124262.94959900 5.4121017456055E-5 0.00%
——————————————————————-
total – 1.5799269676208100.00%
——————————————————————-

and here is the source code I use for test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
$iterations = 1000000;
$a = new SplFastArray($iterations);
$b = array();
for ($i=0;$i<$iterations;$i++) {
$a[$i] = $i;
$b[$i] = $i;
}
require_once 'Benchmark/Timer.php';
$timer = new Benchmark_Timer();
$timer->start();
 
 
foreach($b as $k=>$v) {
}
$timer->setMarker('simple array foreach');
 
foreach($a as $k=>$v) {
}
$timer->setMarker('fast array foreach');
 
 
for ($i=0;$i<count($b);$i++) {
}
$timer->setMarker('simple array for');
 
 
for ($i=0;$i<$a->getSize();$i++) {
}
 
$timer->setMarker('fast array for');
 
$timer->stop();
echo $timer->display();
 
?>

Share/Save/Bookmark

2 Responses to “SplFastArray”

  1. Alex Says:

    Hi,

    can you check this for

    for ($i=0,$l=count($b);$i<$l;$i++) {
    }

  2. ivan Says:

    Actually SplFastArray is faster only for writing. Reading is slower.

Leave a Reply