SimplePHPEasyPlus /benchmark/simple.php

Language PHP Lines 76
MD5 Hash a8209f16e493a988adecd1e761cfd984
Repository https://github.com/waynedog74/SimplePHPEasyPlus.git View Raw File
 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
<?php

use SimplePHPEasyPlus\Number\NumberCollection;
use SimplePHPEasyPlus\Number\SimpleNumber;
use SimplePHPEasyPlus\Number\CollectionItemNumberProxy;
use SimplePHPEasyPlus\Parser\SimpleNumberStringParser;
use SimplePHPEasyPlus\Iterator\CallbackIterator;
use SimplePHPEasyPlus\Operator\AdditionOperator;
use SimplePHPEasyPlus\Operation\ArithmeticOperation;
use SimplePHPEasyPlus\Operation\OperationStream;
use SimplePHPEasyPlus\Engine;
use SimplePHPEasyPlus\Calcul\Calcul;
use SimplePHPEasyPlus\Calcul\CalculRunner;

require_once(__DIR__.'/../tests/bootstrap.php');

function process($a, $b)
{
    $numberCollection = new NumberCollection();

    $numberParser = new SimpleNumberStringParser();

    $firstParsedNumber = $numberParser->parse('2');
    $firstNumber = new SimpleNumber($firstParsedNumber);
    $firstNumberProxy = new CollectionItemNumberProxy($firstNumber);

    $numberCollection->add($firstNumberProxy);

    $secondParsedNumber = $numberParser->parse('3');
    $secondNumber = new SimpleNumber($secondParsedNumber);
    $secondNumberProxy = new CollectionItemNumberProxy($secondNumber);

    $numberCollection->add($secondNumberProxy);

    $addition = new AdditionOperator('SimplePHPEasyPlus\Number\SimpleNumber');

    $operation = new ArithmeticOperation($addition);

    $engine = new Engine($operation);

    $calcul = new Calcul($engine, $numberCollection);

    $runner = new CalculRunner();

    $runner->run($calcul);

    $result = $calcul->getResult();
    $numericResult = $result->getValue();
}

$number = 100000;
$a = $b = 1;

$start = microtime(true);
for($i = 0 ; $i < $number ; ++$i) {
    $a + $b;
}
$time1 = microtime(true) - $start;
printf('For %d additions:%s', $number, "\n");;
printf('    Native PHP "+" operator: %01.2f ms.%s', $time1*$number, "\n");

$start = microtime(true);
for($i = 0 ; $i < $number ; ++$i) {
    process($a, $b);
}
$time2 = microtime(true) - $start;
printf('    SimplePHPEasyPlus: %01.2f ms.%s', $time2*$number, "\n");

printf('%s=> ', "\n");
if ($time2 >= $time1) {
  printf('SimplePHPEasyPlus is %.1fx slower than Native PHP', $time2/$time1);
} else {
  printf('SimplePHPEasyPlus is %.1fx faster than Native PHP', $time1/$time2);
}
printf('%s', "\n");
Back to Top