Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $file = fopen('test.txt', 'r');
- $start = microtime(true);
- $records = 0;
- while ($line = fgets($file)) {
- list($timestamp, $event, $args) = explode(':', $line, 3);
- $args = parse_csv($args);
- $records++;
- }
- fclose($file);
- $sec_duration = microtime(true) - $start;
- $rps = number_format($records / $sec_duration, 2);
- echo "$records processed in $sec_duration seconds at $rps records per second\r\n";
- // returns an array of parsed csv data
- function parse_csv($str) {
- $a = array();
- $quotes = false;
- $cur_val = "";
- $len = strlen($str);
- for ($i = 0; $i < $len; $i++) {
- $c = $str[$i];
- switch ($c) {
- case '"':
- if ($i < ($len - 1) && $str[$i + 1] == '"') {
- $cur_val .= '"';
- $i++;
- } else {
- $quotes = !$quotes;
- }
- break;
- case ',':
- if ($quotes) {
- $cur_val .= $c;
- } else {
- $a[] = $cur_val;
- $cur_val = "";
- }
- break;
- default:
- $cur_val .= $c;
- }
- }
- $a[] = $cur_val;
- return $a;
- }
Add Comment
Please, Sign In to add comment