Skip to content

Instantly share code, notes, and snippets.

@s3b4stian
Created June 9, 2017 15:43
Show Gist options
  • Save s3b4stian/9441af5855b795cc1569b3cdb5e7526d to your computer and use it in GitHub Desktop.
Save s3b4stian/9441af5855b795cc1569b3cdb5e7526d to your computer and use it in GitHub Desktop.
<?php
//change for point to correct directory
include '../linna-array/src/TypedArray.php';
include '../linna-array/src/TypedObjectArray.php';
use Linna\TypedArray;
use Linna\TypedObjectArray;
$cicles = 500000;
//test create new typed array
$time[0] = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
$array = new TypedArray('int');
}
$time[0] += microtime(true);
usleep(100);
//test create new typed object array
$time[1] = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
$array = new TypedObjectArray(SplStack::class);
}
$time[1] += microtime(true);
usleep(100);
//test create new native array object
$time[2] = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
$array = new ArrayObject();
}
$time[2] += microtime(true);
usleep(100);
//test assign element to typed array
$array = new TypedArray('int');
$time[3] = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
$array[] = 1;
}
$time[3] += microtime(true);
usleep(100);
//test assign element to typed object array
$array = new TypedObjectArray(SplStack::class);
$time[4] = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
$array[] = new SplStack();
}
$time[4] += microtime(true);
usleep(100);
//test assign element to array object
$array = new ArrayObject();
$time[5] = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
$array[] = 1;
}
$time[5] += microtime(true);
usleep(100);
//test retrive element from type array
$array = new TypedArray('int');
$array[] = 1;
$time[6] = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
$element = $array[0];
}
$time[6] += microtime(true);
usleep(100);
//test retrive element from type object array
$array = new TypedObjectArray(SplStack::class);
$array[] = new SplStack();
$time[7] = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
$element = $array[0];
}
$time[7] += microtime(true);
usleep(100);
//test retrive element from array object
$array = new ArrayObject();
$array[] = 1;
$time[8] = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
$element = $array[0];
}
$time[8] += microtime(true);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>TypedArray speed test</title>
<style>
table{
font-family: Arial;
border-collapse: collapse;
}
table, td, th {
border:1px solid #ccc;
}
td, th {
padding:5px;
}
</style>
</head>
<body>
<span style="color:red; font-family: Arial;">
Run <?php echo $cicles ?> times<br />
Virtual Box, 1 CPU 40% max., 512 Mb ram<br/>
with Ubuntu 16.04.02, PHP 7.1.5, Apache 2.4 mod php-fpm<br/>
on Intel Core 2 Extreme x9100, 4 Gb ram, SSD samsung 850 evo
</span>
<table>
<tr>
<th><h3>TypedArray</h3></th>
<th><h3>TypedObjectArray</h3></th>
<th><h3>ArrayObject</h3></th>
<th><h3>Speed Gap</h3><span style="font-size:10px;">TypedArray - TypedObjectArray / ArrayObject</span></th>
</tr>
<tr>
<td><pre>$array = new TypedArray('int');</pre><br/><?php echo $time[0] ?> sec.</td>
<td><pre>$array = new TypedObjectArray(SplStack::class);</pre><br/><?php echo $time[1] ?> sec.</td>
<td><pre>$array = new ArrayObject();</pre><br/><?php echo $time[2] ?> sec.</td>
<td style="text-align:center;">
<?php echo round($time[0] / $time[2]).'x - '; ?>
<?php echo round($time[1] / $time[2]).'x'; ?>
</td>
</tr>
<tr>
<td><pre>$array[] = 1;</pre><br/><?php echo $time[3] ?> sec.</td>
<td><pre>$array[] = new SplStack();</pre><br/><?php echo $time[4] ?> sec.</td>
<td><pre>$array[] = 1;</pre><br/><?php echo $time[5] ?> sec.</td>
<td style="text-align:center;">
<?php echo round($time[3] / $time[5]).'x - '; ?>
<?php echo round($time[4] / $time[5]).'x'; ?>
</td>
</tr>
<tr>
<td><pre>$element = $array[0];</pre><br/><?php echo $time[6] ?> sec.</td>
<td><pre>$element = $array[0];</pre><br/><?php echo $time[7] ?> sec.</td>
<td><pre>$element = $array[0];</pre><br/><?php echo $time[8] ?> sec.</td>
<td style="text-align:center;">
<?php echo round($time[6] / $time[8]).'x - '; ?>
<?php echo round($time[7] / $time[8]).'x'; ?>
</td>
</tr>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment