|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +final class DynamicPropertyEntity |
| 6 | +{ |
| 7 | + public int $first = 0; |
| 8 | + public int $second = 0; |
| 9 | + public int $third = 0; |
| 10 | + public int $fourth = 0; |
| 11 | + public int $fifth = 0; |
| 12 | + |
| 13 | + public function hydrate(array $data): void |
| 14 | + { |
| 15 | + foreach ($data as $property => $value) { |
| 16 | + $this->$property = $value; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public function sum(array $properties): int |
| 21 | + { |
| 22 | + $sum = 0; |
| 23 | + foreach ($properties as $property) { |
| 24 | + $sum += $this->$property; |
| 25 | + } |
| 26 | + return $sum; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +final class StaticPropertyEntity |
| 31 | +{ |
| 32 | + public int $first = 0; |
| 33 | + public int $second = 0; |
| 34 | + public int $third = 0; |
| 35 | + public int $fourth = 0; |
| 36 | + public int $fifth = 0; |
| 37 | + |
| 38 | + public function hydrate(array $data): void |
| 39 | + { |
| 40 | + $this->first = $data['first']; |
| 41 | + $this->second = $data['second']; |
| 42 | + $this->third = $data['third']; |
| 43 | + $this->fourth = $data['fourth']; |
| 44 | + $this->fifth = $data['fifth']; |
| 45 | + } |
| 46 | + |
| 47 | + public function sum(): int |
| 48 | + { |
| 49 | + return $this->first + $this->second + $this->third + $this->fourth + $this->fifth; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function runDynamicWrite(DynamicPropertyEntity $entity, array $data, int $iterations): int |
| 54 | +{ |
| 55 | + for ($i = 0; $i < $iterations; $i++) { |
| 56 | + $entity->hydrate($data); |
| 57 | + } |
| 58 | + return $entity->first; |
| 59 | +} |
| 60 | + |
| 61 | +function runStaticWrite(StaticPropertyEntity $entity, array $data, int $iterations): int |
| 62 | +{ |
| 63 | + for ($i = 0; $i < $iterations; $i++) { |
| 64 | + $entity->hydrate($data); |
| 65 | + } |
| 66 | + return $entity->first; |
| 67 | +} |
| 68 | + |
| 69 | +function runDynamicRead(DynamicPropertyEntity $entity, array $properties, int $iterations): int |
| 70 | +{ |
| 71 | + $sum = 0; |
| 72 | + for ($i = 0; $i < $iterations; $i++) { |
| 73 | + $sum += $entity->sum($properties); |
| 74 | + } |
| 75 | + return $sum; |
| 76 | +} |
| 77 | + |
| 78 | +function runStaticRead(StaticPropertyEntity $entity, int $iterations): int |
| 79 | +{ |
| 80 | + $sum = 0; |
| 81 | + for ($i = 0; $i < $iterations; $i++) { |
| 82 | + $sum += $entity->sum(); |
| 83 | + } |
| 84 | + return $sum; |
| 85 | +} |
| 86 | + |
| 87 | +function measure(callable $callback, int $operations): float |
| 88 | +{ |
| 89 | + global $benchmarkSink; |
| 90 | + for ($warmup = 0; $warmup < 3; $warmup++) { |
| 91 | + $benchmarkSink += $callback(); |
| 92 | + } |
| 93 | + |
| 94 | + $best = 1.0e30; |
| 95 | + for ($round = 0; $round < 7; $round++) { |
| 96 | + $start = hrtime(true); |
| 97 | + $result = $callback(); |
| 98 | + $elapsed = hrtime(true) - $start; |
| 99 | + $benchmarkSink += $result; |
| 100 | + if ($elapsed < $best) { |
| 101 | + $best = $elapsed; |
| 102 | + } |
| 103 | + } |
| 104 | + return $best / $operations; |
| 105 | +} |
| 106 | + |
| 107 | +function main(): void |
| 108 | +{ |
| 109 | + global $benchmarkSink; |
| 110 | + $benchmarkSink = 0; |
| 111 | + $iterations = 200000; |
| 112 | + $data = [ |
| 113 | + 'first' => 1, |
| 114 | + 'second' => 2, |
| 115 | + 'third' => 3, |
| 116 | + 'fourth' => 4, |
| 117 | + 'fifth' => 5, |
| 118 | + ]; |
| 119 | + $properties = ['first', 'second', 'third', 'fourth', 'fifth']; |
| 120 | + $dynamic = new DynamicPropertyEntity(); |
| 121 | + $static = new StaticPropertyEntity(); |
| 122 | + $operations = $iterations * 5; |
| 123 | + |
| 124 | + $dynamicWrite = measure( |
| 125 | + function () use ($dynamic, $data, $iterations): int { |
| 126 | + return runDynamicWrite($dynamic, $data, $iterations); |
| 127 | + }, |
| 128 | + $operations, |
| 129 | + ); |
| 130 | + $staticWrite = measure( |
| 131 | + function () use ($static, $data, $iterations): int { |
| 132 | + return runStaticWrite($static, $data, $iterations); |
| 133 | + }, |
| 134 | + $operations, |
| 135 | + ); |
| 136 | + $dynamicRead = measure( |
| 137 | + function () use ($dynamic, $properties, $iterations): int { |
| 138 | + return runDynamicRead($dynamic, $properties, $iterations); |
| 139 | + }, |
| 140 | + $operations, |
| 141 | + ); |
| 142 | + $staticRead = measure( |
| 143 | + function () use ($static, $iterations): int { |
| 144 | + return runStaticRead($static, $iterations); |
| 145 | + }, |
| 146 | + $operations, |
| 147 | + ); |
| 148 | + |
| 149 | + printf("dynamic_write_ns=%.3f\n", $dynamicWrite); |
| 150 | + printf("static_write_ns=%.3f\n", $staticWrite); |
| 151 | + printf("dynamic_read_ns=%.3f\n", $dynamicRead); |
| 152 | + printf("static_read_ns=%.3f\n", $staticRead); |
| 153 | + echo 'checksum=', $benchmarkSink + $dynamic->sum($properties) + $static->sum(), "\n"; |
| 154 | +} |
0 commit comments