Guest User

Untitled

a guest
May 27th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. <?php
  2. $file = fopen('test.txt', 'r');
  3.  
  4. $start = microtime(true);
  5. $records = 0;
  6. while ($line = fgets($file)) {
  7. list($timestamp, $event, $args) = explode(':', $line, 3);
  8. $args = parse_csv($args);
  9.  
  10. $records++;
  11. }
  12.  
  13. fclose($file);
  14.  
  15.  
  16. $sec_duration = microtime(true) - $start;
  17. $rps = number_format($records / $sec_duration, 2);
  18. echo "$records processed in $sec_duration seconds at $rps records per second\r\n";
  19.  
  20. // returns an array of parsed csv data
  21. function parse_csv($str) {
  22. $a = array();
  23. $quotes = false;
  24. $cur_val = "";
  25. $len = strlen($str);
  26.  
  27. for ($i = 0; $i < $len; $i++) {
  28. $c = $str[$i];
  29.  
  30. switch ($c) {
  31. case '"':
  32. if ($i < ($len - 1) && $str[$i + 1] == '"') {
  33. $cur_val .= '"';
  34. $i++;
  35. } else {
  36. $quotes = !$quotes;
  37. }
  38.  
  39. break;
  40. case ',':
  41. if ($quotes) {
  42. $cur_val .= $c;
  43. } else {
  44. $a[] = $cur_val;
  45. $cur_val = "";
  46. }
  47.  
  48. break;
  49. default:
  50. $cur_val .= $c;
  51. }
  52. }
  53.  
  54. $a[] = $cur_val;
  55.  
  56. return $a;
  57. }
Add Comment
Please, Sign In to add comment