note 119511 added to function.fseek

From: Date: Sun, 26 Jun 2016 01:33:27 +0000
Subject: note 119511 added to function.fseek
Groups: php.notes 
Request: Send a blank email to php-notes+get-205813@lists.php.net to get a copy of this message
Seek to a line of code than break from while to improve performance. Seek to specific line using SEEK_SET and get a specific line. If $range is '0' than will show seeked line. If set to '2' it will show current line + 2 lines above + 2 lines below. Useful for get a content of a file in very huge file to get lines range. To improve performance a while loop breaks from iteration than go for seeking. I've created a function that read a file and count lines and store into arrays each lines bytes to seek. If maximum specified by linenum is set, it will break from while to keep performance than in a new loop function to seek a position in bytes to get a content of file. function readFileSeek($source, $linenum = 0, $range = 0) { $fh = fopen($source, 'r'); $meta = stream_get_meta_data($fh); if (!$meta['seekable']) { throw new Exception(sprintf("A source is not seekable: %s", print_r($source, true))); } $pos = 2; $result = null; if ($linenum) { $minline = $linenum - $range - 1; $maxline = $minline+$range+$range; } $totalLines = 0; while (!feof($fh)) { $char = fgetc($fh); if ($char == "\n" || $char == "\r") { ++$totalLines; } else { $result[$totalLines] = $pos; } $pos++; if ($maxline+1 == $totalLines) { // break from while to not read entire file break; } } $buffer = ''; for ($nr=$minline; $nr<=$maxline; $nr++) { if (isset($result[$nr])) { fseek($fh, $result[$nr], SEEK_SET); while (!feof($fh)) { $char = fgetc($fh); if ($char == "\n" || $char == "\r") { $buffer .= $char; break; } else { $buffer .= $char; } } } } return $buffer; } Test results (1.3 GB file, 100000000 lines of codes, seek to 300000 line a code): string(55) "299998_abc 299999_abc 300000_abc 300001_abc 300002_abc " Time: 612 ms, Memory: 20.00Mb $ ll -h /tmp/testfile -rw-rw-r-- 1 1,3G /tmp/testfile ---- Server IP: 72.52.91.14 Probable Submitter: 93.138.94.1 ---- Manual Page -- http://php.net/manual/en/function.fseek.php Edit -- https://master.php.net/note/edit/119511 Del: integrated -- https://master.php.net/note/delete/119511/integrated Del: useless -- https://master.php.net/note/delete/119511/useless Del: bad code -- https://master.php.net/note/delete/119511/bad+code Del: spam -- https://master.php.net/note/delete/119511/spam Del: non-english -- https://master.php.net/note/delete/119511/non-english Del: in docs -- https://master.php.net/note/delete/119511/in+docs Del: other reasons-- https://master.php.net/note/delete/119511 Reject -- https://master.php.net/note/reject/119511 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205813) next »