benchmark-remove-listener-once.php 1021 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of Evenement.
  4. *
  5. * (c) Igor Wiedler <igor@wiedler.ch>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. ini_set('memory_limit', '512M');
  11. const ITERATIONS = 100000;
  12. use Evenement\EventEmitter;
  13. require __DIR__.'/../vendor/autoload.php';
  14. $emitter = new EventEmitter();
  15. $listeners = [];
  16. for ($i = 0; $i < ITERATIONS; $i++) {
  17. $listeners[] = function ($a, $b, $c) {};
  18. }
  19. $start = microtime(true);
  20. foreach ($listeners as $listener) {
  21. $emitter->once('event', $listener);
  22. }
  23. $time = microtime(true) - $start;
  24. echo 'Adding ', number_format(ITERATIONS), ' once listeners took: ', number_format($time, 2), 's', PHP_EOL;
  25. $start = microtime(true);
  26. foreach ($listeners as $listener) {
  27. $emitter->removeListener('event', $listener);
  28. }
  29. $time = microtime(true) - $start;
  30. echo 'Removing ', number_format(ITERATIONS), ' once listeners took: ', number_format($time, 2), 's', PHP_EOL;