Skip to content

Instantly share code, notes, and snippets.

@hrach
Created August 23, 2013 18:22
Show Gist options
  • Save hrach/6322401 to your computer and use it in GitHub Desktop.
Save hrach/6322401 to your computer and use it in GitHub Desktop.
Objekty s preddefinovanymi property jsou pro Entity o polovinu datove narocne nez jedno pole + __get & __set. Objekt si totiz nemusi drzet klic.
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<?php
function calculate_median($arr) {
sort($arr);
$count = count($arr); //total numbers in array
$middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
if($count % 2) { // odd number, middle is the median
$median = $arr[$middleval];
} else { // even number, calculate avg of 2 medians
$low = $arr[$middleval];
$high = $arr[$middleval+1];
$median = (($low+$high)/2);
}
return $median;
}
function calculate_average($arr) {
return array_sum($arr) / count($arr);
}
class Test1 {
public $var1;
public $var2;
public $var3;
public $var4;
public $var5;
}
class Test2 {
public $data = array();
}
$data = array();
$max = 20;
$cols = 5;
$objects = 2000;
function test($type, $cb) {
global $data;
$time = microtime(TRUE);
$mem = memory_get_usage();
$mem2 = $cb();
$data['memory'][$type][] = $mem2 - $mem;
$data['time'][$type][] = microtime(TRUE) - $time;
}
for ($i = 0; $i < $max; $i++) {
test('property', function() use ($cols, $objects) {
$c = array();
for ($i = 0; $i < $objects; $i++) {
$t = new Test1;
for ($j = 1; $j <= $cols; $j++) {
$t->{'var' . $j} = $j;
}
$c[$i] = $t;
}
return memory_get_usage();
});
test('object array', function() use ($cols, $objects) {
$c = array();
for ($i = 0; $i < $objects; $i++) {
$t = new Test2;
for ($j = 1; $j <= $cols; $j++) {
$t->data['var' . $j] = $j;
}
$c[$i] = $t;
}
return memory_get_usage();
});
test('array', function() use ($cols, $objects) {
$c = array();
for ($i = 0; $i < $objects; $i++) {
$t = array();
for ($j = 1; $j <= $cols; $j++) {
$t['var' . $j] = $j;
}
$c[$i] = $t;
}
return memory_get_usage();
});
}
?>
<table class="table table-striped table-hover">
<thead>
<tr>
<th></th>
<th colspan="<?= count($data['memory']) ?>">memory</th>
<th colspan="<?= count($data['time']) ?>">time</th>
</tr>
<tr>
<th></th>
<?php
foreach ($data['memory'] as $type => $val) {
echo '<th>' . $type . '</th>';
}
foreach ($data['time'] as $type => $val) {
echo '<th>' . $type . '</th>';
}
?>
</td>
</thead>
<tbody>
<?php
for ($i = 0; $i < $max; $i++) {
echo '<tr><td>' . $i . '</td>';
foreach ($data['memory'] as $val) {
echo '<td>' . round($val[$i] / 1024 / 1024, 4) . ' MB</td>';
}
foreach ($data['time'] as $val) {
echo '<td>' . round($val[$i] * 1024, 2) . ' ms</td>';
}
echo '</tr>';
}
?>
</tbody>
<tfooter>
<tr class="success">
<td>average</td>
<?php
foreach ($data['memory'] as $val) {
echo '<td>' . round(calculate_average($val) / 1024 / 1024, 4) . ' MB</td>';
}
foreach ($data['time'] as $val) {
echo '<td>' . round(calculate_average($val) * 1024, 2) . ' ms</td>';
}
?>
</tr>
<tr class="success">
<td>median</td>
<?php
foreach ($data['memory'] as $val) {
echo '<td>' . round(calculate_median($val) / 1024 / 1024, 4) . ' MB</td>';
}
foreach ($data['time'] as $val) {
echo '<td>' . round(calculate_median($val) * 1024, 2) . ' ms</td>';
}
?>
</tr>
</tfooter>
</table>
memory time
property object array array property object array array
0 0.6038 MB 1.305 MB 1.168 MB 16.89 ms 14.81 ms 13.14 ms
1 0.5726 MB 1.3051 MB 1.1677 MB 11.6 ms 17.17 ms 12.73 ms
2 0.5726 MB 1.3051 MB 1.1677 MB 13.07 ms 16.11 ms 14.97 ms
3 0.5726 MB 1.3051 MB 1.1678 MB 14.73 ms 14.52 ms 11.66 ms
4 0.5726 MB 1.3051 MB 1.1678 MB 13.86 ms 11.9 ms 10.18 ms
5 0.5726 MB 1.305 MB 1.1678 MB 11.82 ms 12.43 ms 10.83 ms
6 0.5727 MB 1.3051 MB 1.1678 MB 13.54 ms 14.25 ms 10.16 ms
7 0.5727 MB 1.3051 MB 1.1679 MB 14.46 ms 18.19 ms 11.55 ms
8 0.5727 MB 1.3051 MB 1.1679 MB 10.8 ms 12.4 ms 11.28 ms
9 0.5727 MB 1.3051 MB 1.1678 MB 13.81 ms 12.86 ms 12.66 ms
10 0.5727 MB 1.305 MB 1.1678 MB 16.34 ms 17.06 ms 13.54 ms
11 0.5727 MB 1.3051 MB 1.1678 MB 12.87 ms 16.13 ms 13.49 ms
12 0.5727 MB 1.3051 MB 1.1677 MB 13.59 ms 13.42 ms 13.08 ms
13 0.5727 MB 1.305 MB 1.1679 MB 11.58 ms 13.08 ms 11.24 ms
14 0.5727 MB 1.3051 MB 1.1678 MB 14.61 ms 13.56 ms 13.08 ms
15 0.5727 MB 1.305 MB 1.1679 MB 10.94 ms 13.01 ms 11.12 ms
16 0.5727 MB 1.3051 MB 1.1679 MB 11.31 ms 13.8 ms 10.92 ms
17 0.5727 MB 1.3051 MB 1.1678 MB 10.87 ms 11.67 ms 10.07 ms
18 0.5727 MB 1.305 MB 1.1678 MB 12.43 ms 12.96 ms 10.31 ms
19 0.5727 MB 1.3051 MB 1.1679 MB 10.69 ms 13.42 ms 11.34 ms
average 0.5742 MB 1.3051 MB 1.1678 MB 12.99 ms 14.14 ms 11.87 ms
median 0.5727 MB 1.3051 MB 1.1678 MB 12.97 ms 13.49 ms 11.45 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment