Advertisement
Guest User

Untitled

a guest
Apr 5th, 2021
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.49 KB | None | 0 0
  1. <?php
  2.  
  3. function t1($hay, $needle, $replacement) {
  4.     return str_replace($needle, $replacement, $hay);
  5. }
  6. function t2($hay, $needle, $replacement) {
  7.     return strtr($hay, [$needle => $replacement]);
  8. }
  9.  
  10. function bench($fn, $n=1000000) {
  11.     $start = microtime(true);
  12.     $s = str_repeat('aaaahibbbb', 1000);
  13.     for ($i = 0; $i < $n; ++$i) {
  14.         $res = $fn($s, 'hi', 'hello');
  15.     }
  16.  
  17.     echo (microtime(true) - $start), PHP_EOL;
  18.     return $res;
  19. }
  20.  
  21. bench('t1');
  22. bench('t2');
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement