Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Number of line in a text file
Replies: 12   Last Post: Jan 23, 2013 1:51 PM

 Messages: [ Previous | Next ]
 Kirill Posts: 139 Registered: 3/28/06
Re: Number of line in a text file
Posted: Nov 2, 2006 11:48 AM

To illustrate this idea the following code creates ~100M text file and
counts lines by fgetl() and by reading in the big chunks (~1M). You
can see that in the first case you need 457 seconds and in the second
about 7 seconds - 65 times more!

Kirill

clc

% write N lines
fname = 'test.txt';
N = 1e7;
fh = fopen(fname, 'w');
for i = 1:N
fprintf(fh, '%010d\n', i);
end
fclose(fh);

% count by fgetl()
tic
fh = fopen(fname, 'r');
n1 = 0;
while ~feof(fh)
s = fgetl(fh);
n1 = n1 + 1;
end
fclose(fh);
toc

% count by chunks
tic
fh = fopen(fname, 'r');
chunksize = 1e6;
n2 = 0;
while ~feof(fh)
if isempty(ch)
break
end
n2 = n2 + sum(ch == char(10));
end
fclose(fh);
toc

N
n1
n2

==========================================

Elapsed time is 457.312000 seconds.
Elapsed time is 7.531000 seconds.

N =

10000000

n1 =

10000000

n2 =

10000000

Date Subject Author
11/1/06 Guest
11/1/06 Kirill
11/2/06 Benoit Valley
11/2/06 JWagberg
11/2/06 Kirill
11/2/06 JWagberg
11/2/06 Kirill
11/3/06 Benoit Valley
11/10/06 Benoit Valley
11/10/06 JWagberg
1/23/13 Leeward.xie
1/23/13 dpb
1/23/13 Derek Goring