Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.