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) ch = fread(fh, chunksize, 'uchar'); 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
|
|