-
Notifications
You must be signed in to change notification settings - Fork 1
/
BigMathBenchmarkTest.php
118 lines (110 loc) · 2.63 KB
/
BigMathBenchmarkTest.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Aza\Components\Math\Tests;
use Aza\Components\Benchmark;
use Aza\Components\Common\Date;
use Aza\Components\Math\BigMath\BCMath;
use Aza\Components\Math\BigMath\GMPMath;
use Aza\Components\Math\BigMath\PHPMath;
use PHPUnit_Framework_TestCase as TestCase;
/**
* Simple math for big integers benchmarks
*
* @project Anizoptera CMF
* @package system.math
* @subpackage BigMath
* @author Amal Samally <amal.samally at gmail.com>
* @license MIT
*
* @group benchmark
* @coversNothing
*
* @requires extension bcmath
* @requires extension gmp
*/
class BigMathBenchmarkTest extends TestCase
{
/**
* Test realizations speed
*
* @author amal
*/
public function testRealizationsSpeed()
{
/**
* Check current bc scale
*
* BCMath
* + Fastest variant
*
* GMPMath
* - ~50% slower than fastest
*
* PHPMath
* - ~5000% slower than fastest
*/
$iteratons = 100;
$tests = 5;
$data = array(
array('1000', '1000'),
array('1324546674576580', '1324546674576580'),
array('13245466745765801324546674576580', '13245466745765801324546674576580'),
array('1', '1'),
array('11', '11'),
array('1111111111', '1111111111'),
array('555', '555'),
array('-10', '10'),
array('10', '-10'),
array('-10', '-10'),
array('0', '0'),
array('5', '0'),
array('-5', '6'),
array('0', '6'),
array('2147483647', '9223372036854775808',),
array('18446744073709551615', '100000000000'),
array('200', '250'),
array('10', '300'),
array('-1', '-1'),
array('-5', '5'),
array('5', '-5'),
array('5', '6'),
);
$gmp = new GMPMath();
$bc = new BCMath();
$native = new PHPMath();
$res = array();
for ($j = 0; $j < $tests; $j++) {
$start = microtime(true);
for ($i = 0; $i < $iteratons; $i++) {
foreach ($data as $d) {
$bc->add($d[0], $d[1]);
$bc->subtract($d[0], $d[1]);
$bc->add($d[1], $d[0]);
$bc->subtract($d[1], $d[0]);
}
}
$res['BCMath'][] = Date::timeEnd($start);
$start = microtime(true);
for ($i = 0; $i < $iteratons; $i++) {
foreach ($data as $d) {
$gmp->add($d[0], $d[1]);
$gmp->subtract($d[0], $d[1]);
$gmp->add($d[1], $d[0]);
$gmp->subtract($d[1], $d[0]);
}
}
$res['GMPMath'][] = Date::timeEnd($start);
$start = microtime(true);
for ($i = 0; $i < $iteratons; $i++) {
foreach ($data as $d) {
$native->add($d[0], $d[1]);
$native->subtract($d[0], $d[1]);
$native->add($d[1], $d[0]);
$native->subtract($d[1], $d[0]);
}
}
$res['PHPMath'][] = Date::timeEnd($start);
}
$results = Benchmark::analyzeResults($res);
print_r($results);
}
}