Thread: how quick using binary read file ?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    how quick using binary read file ?

    Code:
    fopen("a.txt","r")
    fopen("a.txt","rb");
    "r" and "rb" , which is more quick? why?

    if I use "rb", must I use fread function?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, binary file mode is quicker, and you lose no clarity of the code, by using it. Recommended for speed critical code. How much faster depends on your everything from your data speed, to your HD, to your cpu and RAM and etc. If I had to guess, I'd say 4% faster than text file mode, depending on the size of the file, also.

    Binary files have no translation involved with them being read. "What you got is what you get".

    Text files have some work that must be done before you can work with them.

    Well, that's a hell of a question! What's wrong with fread(), anyway?
    Last edited by Adak; 01-12-2010 at 03:02 AM.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Adak View Post
    Yes, binary file mode is quicker, and you lose no clarity of the code, by using it. Recommended for speed critical code. How much faster depends on your everything from your data speed, to your HD, to your cpu and RAM and etc. If I had to guess, I'd say 4% faster than text file mode, depending on the size of the file, also.

    Binary files have no translation involved with them being read. "What you got is what you get".

    Text files have some work that must be done before you can work with them.

    Well, that's a hell of a question! What's wrong with fread(), anyway?
    In same conditions ( same HD same CPU and etc) , how much faster?
    almost 4% than text file mode ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zcrself
    In same conditions ( same HD same CPU and etc) , how much faster?
    almost 4% than text file mode ?
    If it matters, measure.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It depends on how many newlines, etc., must be translated. I would guess 4% faster, but run your own tests. Short files, long files, files with lots of newlines, files with very few. You must test to be sure.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But bear in mind that file I/O is vastly slower than reading from memory, so your average processor isn't going to be over troubled by removing a few newlines from a block of memory (compared to the time it takes the disk to rotate the next sector under the read head).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read a file of commands and execute them
    By surlyTomato in forum C Programming
    Replies: 5
    Last Post: 08-20-2009, 11:32 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Binary file is read with bytes missing
    By thomas41546 in forum C# Programming
    Replies: 1
    Last Post: 09-03-2006, 04:15 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM