The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

Some programming help by eof 2005-01-27 05:30:17

Hi, I got this exercise I have to do and was hoping for a little help... don't worry, I don't want the solution, I'm merely hoping for suggestions in the way of optimization. :)

Basic problem: Open a file and determine its length by way of counting the number of characters (bytes) in it. IOW, not a hard problem at all. It is to be written in C, which I haven't used for a very long time--due to circumstances, I've lately been using Java.

Anyway. At least in Java, reading (counting) the characters one by one is, of course, terribly inefficient. Just for the fun of it, I wrote some short snippets in Java--one that reads character by character, another that prepares a buffer and reads into it. For a large file, the former took about 600+ times longer to complete than the latter.

Question #1: I assume it is safe to assume the same for C (meaning that reading into some sort of buffer is the way to go)?

Question #2: What would typically be a good size for the buffer? At least in Java, smaller buffers are somewhat less efficient but at some point larger buffers seem to slow down the process rather than speed it up. As an example, with a ~107MB file I got the best performance using a 20K buffer, and using a 1K buffer took about 50% longer; ditto using a 40K buffer. (Buffers were allocated before I started the timer--the only thing I timed was the actual reading.) This would indicate that 20K (in Java) is a good size for a buffer for this purpose. Opinions?

Question #3: Would it be safe to assume that a similar buffer size will be fairly efficient in C as well, and if not, what would be?

Question #4: In Java, since I really am not interested in what actually is read from the file, is there some sort of "buffer" that I could use that would be quicker (essentially, "I don't care of the contents, just tell me how many bytes you've read and drop the bytes")? (This doesn't seem functionally useful to me, so I'd assume the answer is no.)

Question #5: In C, I suppose similar functionality is accomplished using fgets (and then "count" the bytes read using the strlen function)? Or is there some "better" solution, possibly one that would "drop" the read data (as described under question #4) and just return how many bytes were read? (Again, I assume the answer is no.)

[ Reply ]
  C has commands that... by jdelphiki2005-01-27 05:38:47
    Yes I know but... by eof2005-01-27 06:53:36
  Some answers by Hessofanes2005-01-27 07:02:09
    Thanks... by eof2005-01-27 08:00:33
      Which one? by Hessofanes2005-01-27 11:44:11
        read.... by eof2005-01-27 22:15:56

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)