473,508 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading large text files in reverse - optimization doubts

Hi folks,
Suppose I have a large (1 GB) text file which I want to read in
reverse. The number of characters I want to read at a time is
insignificant. I'm confused as to how best to do it. Upon browsing
through this group and other sources on the web, it seems that there
are many ways to do it.
Some suggest that simply fseek'ing to 8K bytes before the end of file,
and going backwards is the way. In this case, am I guaranteed best
results if I fseek 8192 bytes behind my last position simply because
BUFSIZ = 8192 ? In other words is 8192 an optimal number ?
Others suggest that mmap'ing some 20-30K at a time into RAM is the way
to go... (but why 20-30K) ?

Another twist in the tale is that my program must be as portable as
possible. So I'm wondering whether mmap'ing would be viable at all -
but if it works fine on *X and Win (does Win even have mmap or an
equivalent ?), then it would be okay.

Any ideas on how to optimize for speed? I'll head off to do some
profiling on Linux and WinXP.. but it needs to be as portable+fast on
as many OS's as possible - so I need you guys' expertise :)

Thanks in advance,
Raj

Nov 15 '05 #1
6 6321
Rajorshi Biswas wrote:
Hi folks,
Suppose I have a large (1 GB) text file which I want to read in
reverse. The number of characters I want to read at a time is
insignificant. I'm confused as to how best to do it. Upon browsing
through this group and other sources on the web, it seems that there
are many ways to do it.
This is not, strictly speaking, a C question. There's nothing in the
language to answer a question like this.
Some suggest that simply fseek'ing to 8K bytes before the end of file,
and going backwards is the way. In this case, am I guaranteed best
results if I fseek 8192 bytes behind my last position simply because
BUFSIZ = 8192 ? In other words is 8192 an optimal number ?
It might be. Or not. If your system page size happens to be 8K, it might
be a good size to read.
Others suggest that mmap'ing some 20-30K at a time into RAM is the way
to go... (but why 20-30K) ?
Just because. I sincerely doubt anyone has calculated that these numbers
are optimal. Clearly you'll want a buffer "as large as possible", but
not so large that you're wasting I/O time reading data you're not
touching for a long time. Hence a few system pages worth of data seems
reasonable.
Another twist in the tale is that my program must be as portable as
possible. So I'm wondering whether mmap'ing would be viable at all -
but if it works fine on *X and Win (does Win even have mmap or an
equivalent ?), then it would be okay.
Yes, Win32 has memory-mapped files. To make matters easier, however,
there is of course no function called "mmap". I recommend using MinGW
(Cygwin's too slow), which will allow you to pretend you're using a
POSIX system, more or less. Otherwise you'll have to write your own
wrappers around CreateFileMapping() and MapViewOfFile[Ex]().
Any ideas on how to optimize for speed? I'll head off to do some
profiling on Linux and WinXP.. but it needs to be as portable+fast on
as many OS's as possible - so I need you guys' expertise :)

Reading a file in reverse will be one of the most inefficient operations
on any OS, I'd wager. If you use the standard I/O library, you're at the
mercy of both the platform and the C library used -- they can make a lot
of difference individually and in tandem. The "best" buffer size,
whether fread()ing or mmap()ing, will depend on the OS, the library, the
file, the time needed to process the data read, the system load...
Profiling is indeed the way to go here. It's too easy to predict
"reasonable" outcomes that will turn out to be entirely wrong in practice.

Clean code that uses either fseek()/fread() or mmap() should run
"reasonably well" on most platforms. The only thing that might trip you
up is an implementation that hits pathological performance when reading
in reverse, but I'd expect those to be rare.

S.
Nov 15 '05 #2
"Rajorshi Biswas" <ra*************@gmail.com> wrote:
# Hi folks,
# Suppose I have a large (1 GB) text file which I want to read in
# reverse. The number of characters I want to read at a time is
# insignificant. I'm confused as to how best to do it. Upon browsing
# through this group and other sources on the web, it seems that there
# are many ways to do it.
# Some suggest that simply fseek'ing to 8K bytes before the end of file,
# and going backwards is the way. In this case, am I guaranteed best
# results if I fseek 8192 bytes behind my last position simply because
# BUFSIZ = 8192 ? In other words is 8192 an optimal number ?

stdio implementations often don't like fseek and tend to discard
the entire buffer and refill after a fseek. Theoretically you
can seek to the end, fseek back one character and read it, fseek
back two characters and read the second last, etc, all the way
to the beginning. However it's likely stdio will be reading in
as much of BUFSIZ as possible with each get character, so you
work around buffering instead of with it.

# Others suggest that mmap'ing some 20-30K at a time into RAM is the way
# to go... (but why 20-30K) ?

Not all virtual memories can map in a gigabyte.

# Any ideas on how to optimize for speed? I'll head off to do some
# profiling on Linux and WinXP.. but it needs to be as portable+fast on
# as many OS's as possible - so I need you guys' expertise :)

You can turn off bufferring with stdio, and then do your own
page frame management. Same thing as mmap, but slower and a
greater nuisance.

You can read in blocks front to back, reverse the block in
memory, and then write the blocks to a secondary file back
to front. You then have a transpose of the original file
that you can read in the forward direction and make the
kernel and stdio happier.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Wow. A sailboat.
Nov 15 '05 #3
On 24 Sep 2005 06:07:46 -0700, in comp.lang.c , "Rajorshi Biswas"
<ra*************@gmail.com> wrote:
Hi folks,
Suppose I have a large (1 GB) text file which I want to read in
reverse.
Actually, I'd recommend you review why on earth you have text files
that large. Logfiles perhaps? Some redesign of the creating app is in
order, to break its logs into sensible sizes.
BUFSIZ = 8192 ? In other words is 8192 an optimal number ?
Yes and no. It depends completely on your h/w. 8K may be a memory
page, or a disk sector size or something, in which case maybe its
optimal. .
Others suggest that mmap'ing some 20-30K at a time into RAM is the way
to go... (but why 20-30K) ?
Same answer.
Another twist in the tale is that my program must be as portable as
possible. So I'm wondering whether mmap'ing would be viable at all -
but if it works fine on *X and Win (does Win even have mmap or an
equivalent ?), then it would be okay
mmap is be a nonstandard function so theres no knowing. If you can
find a compiler portable across your required h/w and osen, you may
also have a portable version of this function
Any ideas on how to optimize for speed?


Read up on the three rules of optimization.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #4
In article <43***********************@news.xs4all.nl>
Skarmander <in*****@dontmailme.com> wrote:
Reading a file in reverse will be one of the most inefficient operations
on any OS, I'd wager.


That would depend on the OS and file system. A file system
implemented via a B-tree with sideways links (B+ or "B-link" or
any of those variations) could be read backwards quite quickly.
A file system designed for use on a DVR/PVR might also use a doubly
linked list, something like the old DOS FAT file system (i.e.
cluster or extent based) except with links going both directions.

The stdio I wrote for BSD (found in 4.4BSD derivatives) also
attempts to optimize fseek() calls on files opened in read-only
mode, so that the sequence:

/* ensure that file is open and contains at least 1 byte */

fseek(fp, -1L, SEEK_END);
for (;;) {
c = getc(fp);
/* do something with c, e.g., putc(c) */
if (ftell(fp) == 0)
break;
fseek(fp, -2L, SEEK_CUR);
}

behaves in a reasonably fast manner, reading one underlying file
system block at a time and otherwise working entirely within the
stdio buffer.

Reading the blocks backwards is not particularly hard on a Unix-like
file system (with inodes containing direct and indirect blocks),
although it does not play well with the OS's read-ahead mechanism.
It *is* particularly hard on the DOS FAT file system, which --
through a design aimed at saving space on 360 kilobyte floppies --
makes it impossible to find block N of any given file without first
finding blocks zero through N-1 inclusive. Modern file systems
(NTFS, ext2, ext3, Reiser, etc) are as good as Version 7 Unix
though, having finally caught up to the 1970s. :-) Some are even
worth of the 1980s, handling files larger than four gigabytes! :-)

(To explain the amusement here, I should add that UFS -- the 4.2BSD
file system that came out in 1983 or 1984 or so -- did all this,
way back then. The newer Linux file systems do have other merits,
but it does seem odd that only recently have other desktop OSes
acquired capabilities we had back in the mid-1980s.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 15 '05 #5
Hmm...
Thanks for the inputs guys. Well I tried an implementation with
fseek/fread with a read chunk size of 8k, also with 16k.. both are
reasonably fast.. at least on Linux. One related doubt I had was
whether fseek and mmap have any advantage over fseek and fread. For the
sake of portability, I discarded the lseek/mmap route, but was just
curious whether mmap would provide any better results.

<ducking for cover>
.... so - profiling is the only way to find out, eh ?
</ducking for cover>

Nov 15 '05 #6
Chris Torek wrote:
In article <43***********************@news.xs4all.nl>
Skarmander <in*****@dontmailme.com> wrote:
Reading a file in reverse will be one of the most inefficient operations
on any OS, I'd wager.

That would depend on the OS and file system. A file system
implemented via a B-tree with sideways links (B+ or "B-link" or
any of those variations) could be read backwards quite quickly.
A file system designed for use on a DVR/PVR might also use a doubly
linked list, something like the old DOS FAT file system (i.e.
cluster or extent based) except with links going both directions.


In the days of 9 track tape, maybe even 7 track tape, it was not
uncommon for tape drives to have the ability to read a tape
backwards. The data was loaded into memory from high address down
to low address, such that the block came out normally in memory.

This ability has been lost in many of the currently popular
drives, especially in helical scan drives.

It was mostly useful in the case of external sorts on tape,
where it saved a rewind between writing the tape and reading
the data back for the next pass.

-- glen

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
17411
by: Jay | last post by:
I have a very large text file (being read by a CGI script on a web server), and I get memory errors when I try to read the whole file into a list of strings. The problem is, I want to read the file...
20
32995
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
50
4879
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
4
5958
by: Matthew Crema | last post by:
Hello, Say I have 1000 text files and each is a list of 32768 integers. I have written a C program to read this data into a large matrix. I am using fopen in combination with fscanf to read...
1
1935
by: Hutty | last post by:
I have a program that open text files and compares them, however, when reading files larger than 500kb the programs seems to bomb. I get re-directed to "page not found". Any idea how to get...
2
4607
by: gauravkhanna | last post by:
Hi All I need some help for the below problem: Scenario We need to send large binary files (audio file of about 10 MB or so) from the client machine (.Net Windows based application, located...
10
6162
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi, I am trying to come up with a c style string reverser, I want it to take 1 argument Altough I would never do this in real life. Is there a way to do it? I wrote this function that fails : ...
9
1811
by: Bill Woessner | last post by:
Suppose I have a structure, foo, which is a POD. I would like to read and write it to disk as follows: std::ofstream outs; foo bar; outs.write(reinterpret_cast<char*>(&bar), sizeof(foo));...
1
1566
by: akalmand | last post by:
Hi there, I am writing a code to read some data from the text files. The number of text files is not fixed and could be more that 15. the length of each file is large... close to 100,000 on an...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.