Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Fastest way to get the number of lines
Date: Wed, 17 Sep 2008 11:28:02 +0000 (UTC)
Organization: ASML Netherlands B.V.
Lines: 13
Message-ID: <gaqpk1$joe$1@fred.mathworks.com>
References: <g8v5cv$1us$1@fred.mathworks.com> <Z8Gsk.114613$nD.80388@pd7urf1no> <g9037b$efl$1@fred.mathworks.com> <g90jtn$p9g$1@z-news.pwr.wroc.pl> <gap9vn$jqf$1@fred.mathworks.com> <cfWzk.11670$rV4.5062@newsfe03.iad>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1221650882 20238 172.30.248.35 (17 Sep 2008 11:28:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 17 Sep 2008 11:28:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 850848
Xref: news.mathworks.com comp.soft-sys.matlab:490588


Here's my take :

fh = fopen(filename, 'r');
chunksize = 1e6; % read chuncks of 1MB at a time
n2 = 0;
while ~feof(fh)
    ch = fread(fh, chunksize, '*uchar');
    if isempty(ch)
        break
    end
    numlines = numlines + sum(ch == sprintf('\n'));
end
fclose(fh);