Article View: comp.std.c
Article #4568Re: Faster FILE subroutines in standard C?
From: Carl Wyles
Date: Fri, 05 Jan 1996 16:13
Date: Fri, 05 Jan 1996 16:13
39 lines
2272 bytes
2272 bytes
ken@yorku.ca (Kenneth Kafieh) writes: >I had to resort to drastic mesures inorder to put this message here. >I found that I was able to get 3-4 time faster performance by using good >old ANSI C file accessing routines (like fgets, fseek, ftell, ...etc.) >instead of C++ streams (like ifstream::getline, fstream::seekg, ...etc.) >I wrote the program originally in C++ (a simple little phonebook for my >own use) and it took about 9 seconds to scan a 100Kb file for a 3 byte >string!!! Fed up, I rewrote it in C and voila! Now it takes 2 secs! >I'm using Borland C++ v2.0. >Does that sound right? Maybe my code was inefficiently written? >I know C++ is generally slower than C but I didn't think that >file stuff would be too. What did I do wrong? Am I missing >something. Maybe I'll post the code too, later. Has anyone >else noticed this too? I think that is about right. C++ file routines can be a little slow... If the record length is more than the length of the data you are searching for, I would suggest putting a sorted index on the file. I have a fortune cookie program I wrote that currently is 1.4Mb long and has 10,000+ fortunes. I implemented a index on it, and made a file about 41k long. It took about 5 minutes to create the index from scratch, but much less than a second to pick any particular fortune. Eg almost as soon as my finger clears the Enter key it prints a fortune. Use a binary search on the index, get a pointer to the data file, and bingo you will get the record in question. Otherwise; Sort the data and use a binary search on it. Carl -- >>>>> All flames sent to Demon Dragon familiar for appropriate response <<<<< cwyles@nyx10.cs.du.edu | My words are the amalgamation of my experiences only Good advice is ignored. Ok advice is used. Bad advice is flamed forever.
Technical Details
Message-ID:
<4cjin2$1bp@nyx10.cs.du.edu>Path:
arc1-ca.pugleaf.net!nyx10.cs.du.edu!not-for-mailReferences:
<4cfm7c$dot@sunburst.ccs.yorku.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!nyx10.cs.du.edu!not-for-mail From: cwyles@nyx10.cs.du.edu (Carl Wyles) Newsgroups: comp.std.c Subject: Re: Faster FILE subroutines in standard C? Date: 5 Jan 1996 09:13:22 -0700 Organization: University of Denver, Math/CS Dept. Message-ID: <4cjin2$1bp@nyx10.cs.du.edu> References: <4cfm7c$dot@sunburst.ccs.yorku.ca> NNTP-Posting-Host: nyx10.cs.du.edu X-Newsreader: NN version 6.5.0 #3 (NOV)