forked from jbboehr/php-mustache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.php
95 lines (86 loc) · 2.87 KB
/
bench.php
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
$count = 100000;
$startMemory = memory_get_usage(true);
$tmplStr = '{{test}}';
$dataArr = array(
'test' => 'val',
);
//$tmplStr = '{{#test}}{{var}}{{/test}}';
//$dataArr = array(
// 'test' => array(
// 'val1',
// 'val2',
// 'val3',
// 'val4',
// 'val5',
// ),
//);
printf('%10s %10s %10s %10s %10s' . PHP_EOL, 'Type', 'Total (s)', 'Each (s)', 'Count', 'Memory Delta (B)');
// Normal
$name = 'Normal';
$startMemory = memory_get_usage(true);
$start = microtime(true);
$mustache = new Mustache();
for( $i = 0; $i < $count; $i++ ) {
$out = $mustache->render($tmplStr, $dataArr);
}
$stop = microtime(true);
$stopMemory = memory_get_usage(true);
printf('%10s %10f %10f %10d %10s' . PHP_EOL,
$name, ($stop - $start), (($stop - $start) / $count), $count, number_format($stopMemory - $startMemory));
// Parsed
$name = 'Parsed';
$startMemory = memory_get_usage(true);
$start = microtime(true);
$mustache = new Mustache();
$ret = $mustache->parse($tmplStr);
for( $i = 0; $i < $count; $i++ ) {
$out = $mustache->render($ret, $dataArr);
}
$stop = microtime(true);
$stopMemory = memory_get_usage(true);
printf('%10s %10f %10f %10d %10s' . PHP_EOL,
$name, ($stop - $start), (($stop - $start) / $count), $count, number_format($stopMemory - $startMemory));
// Parsed+Data
$name = 'Parsed2';
$startMemory = memory_get_usage(true);
$start = microtime(true);
$tmpl = $mustache->parse($tmplStr);
$data = new MustacheData($dataArr);
for( $i = 0; $i < $count; $i++ ) {
$out = $mustache->render($tmpl, $data);
}
$stop = microtime(true);
$stopMemory = memory_get_usage(true);
printf('%10s %10f %10f %10d %10s' . PHP_EOL,
$name, ($stop - $start), (($stop - $start) / $count), $count, number_format($stopMemory - $startMemory));
// Compiled
$name = 'Compiled';
$startMemory = memory_get_usage(true);
$start = microtime(true);
$mustache = new Mustache();
$ret = $mustache->compile($tmplStr);
for( $i = 0; $i < $count; $i++ ) {
$out = $mustache->execute($ret, $dataArr);
}
$stop = microtime(true);
$stopMemory = memory_get_usage(true);
printf('%10s %10f %10f %10d %10s' . PHP_EOL,
$name, ($stop - $start), (($stop - $start) / $count), $count, number_format($stopMemory - $startMemory));
// Compiled+Data
$name = 'Compiled2';
$startMemory = memory_get_usage(true);
$start = microtime(true);
$tmpl = $mustache->compile($tmplStr);
$data = new MustacheData($dataArr);
for( $i = 0; $i < $count; $i++ ) {
$out = $mustache->execute($tmpl, $data);
}
$stop = microtime(true);
$stopMemory = memory_get_usage(true);
printf('%10s %10f %10f %10d %10s' . PHP_EOL,
$name, ($stop - $start), (($stop - $start) / $count), $count, number_format($stopMemory - $startMemory));
echo PHP_EOL;
printf("Memory (start): %s" . PHP_EOL, number_format($startMemory));
printf("Memory (end): %s" . PHP_EOL, number_format(memory_get_usage(true)));
printf("Memory (peak): %s" . PHP_EOL, number_format(memory_get_peak_usage(true)));