fork download
  1. <?php
  2.  
  3. // -------------------------------------
  4. // test file generator
  5.  
  6. $filename = 'test.txt';
  7. $pattern = '--' . "\r\n";
  8. $count_lines = 100000; //100k
  9.  
  10. for ($i=0; $i < $count_lines; $i++) {
  11. $lines[] = $pattern;
  12. }
  13.  
  14. file_put_contents($filename, $lines);
  15.  
  16.  
  17. // -------------------------------------
  18. // comparison script
  19.  
  20. $pattern = "\r\n";
  21. $new_pattern = '<br>';
  22. $lines = file('test.txt');
  23.  
  24. $str_start = xdebug_time_index();
  25. foreach ($lines as $line) {
  26. $str_lines[] = str_replace($pattern, $new_pattern, $line);
  27. }
  28. $str_end = xdebug_time_index();
  29.  
  30. $preg_start = xdebug_time_index();
  31. foreach ($lines as $line) {
  32. $preg_lines[] = preg_replace("#{$pattern}#", $new_pattern, $line);
  33. }
  34. $preg_end = xdebug_time_index();
  35.  
  36. echo ('str_replace time = ' . round($str_end - $str_start, 5));
  37. echo '<br>';
  38. echo ('preg_replace time = ' . round(($preg_end - $preg_start), 5));
  39.  
  40. file_put_contents('str.txt', $str_lines);
  41. file_put_contents('preg.txt', $preg_lines);
Runtime error #stdin #stdout #stderr 0.01s 82560KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
PHP Warning:  file_put_contents(test.txt): failed to open stream: Permission denied in /home/oWVWdR/prog.php on line 14
PHP Warning:  file(test.txt): failed to open stream: No such file or directory in /home/oWVWdR/prog.php on line 22
PHP Fatal error:  Uncaught Error: Call to undefined function xdebug_time_index() in /home/oWVWdR/prog.php:24
Stack trace:
#0 {main}
  thrown in /home/oWVWdR/prog.php on line 24