11<?php
2+
23declare (strict_types=1 );
34
45namespace Danon \IntervalTree \Tests \Benchmark ;
56
67use Danon \IntervalTree \Interval \NumericInterval ;
78use Danon \IntervalTree \IntervalTree ;
8- use Exception ;
99use PhpBench \Benchmark \Metadata \Annotations \Revs ;
1010
1111/**
@@ -35,7 +35,7 @@ public function init(): void
3535 $ this ->bruteForceList = [];
3636
3737 for ($ i = 0 ; $ i < self ::AMOUNT_INTERVALS_IN_TREE ; $ i ++) {
38- $ interval = $ this ->generateInterval ();
38+ $ interval = $ this ->generateInterval (self :: MAX_INTERVAL_HIGH , self :: MAX_INTERVAL_OFFSET );
3939 $ this ->tree ->insert ($ interval );
4040 $ this ->bruteForceList [] = $ interval ;
4141 }
@@ -46,7 +46,7 @@ public function init(): void
4646 */
4747 public function benchTree (): void
4848 {
49- $ searchedInterval = $ this ->generateInterval ();
49+ $ searchedInterval = $ this ->generateInterval (self :: MAX_INTERVAL_HIGH , self :: MAX_INTERVAL_OFFSET );
5050 $ this ->tree ->hasIntersection ($ searchedInterval );
5151 }
5252
@@ -55,26 +55,11 @@ public function benchTree(): void
5555 */
5656 public function benchBruteForce (): void
5757 {
58- $ searchedInterval = $ this ->generateInterval ();
58+ $ searchedInterval = $ this ->generateInterval (self :: MAX_INTERVAL_HIGH , self :: MAX_INTERVAL_OFFSET );
5959 foreach ($ this ->bruteForceList as $ interval ) {
6060 if ($ interval ->intersect ($ searchedInterval )) {
6161 break ;
6262 }
6363 }
6464 }
65-
66- /**
67- * @return NumericInterval
68- */
69- private function generateInterval (): NumericInterval
70- {
71- try {
72- $ low = random_int (0 , self ::MAX_INTERVAL_HIGH );
73- $ high = random_int ($ low , min ($ low + self ::MAX_INTERVAL_OFFSET , self ::MAX_INTERVAL_HIGH ));
74- } catch (Exception $ exception ) {
75- echo 'Cannot generate interval: ' . $ exception ->getMessage ();
76- exit ;
77- }
78- return new NumericInterval ($ low , $ high );
79- }
8065}
0 commit comments