Common methods and comparison of reading files

Source: Internet
Author: User
The input stream is read by byte directly without the format.
 
 
 
The test file is a 24 m ENGLISH file.
 
 
C language, fread
 
# Include <stdio. h>IntMain (){CharC [1];IntNum; file * fp = fopen ("Http://www.cnblogs.com/big.log","RB");While(1) {num = fread (C, 1, 1, FP); // each read-only char, the first 1 indicates one read byte, the second value is 1 consecutive read.// Printf ("% C", C [0]);If(Num = 0)Break;}Return0 ;}
 
Running time

Real 0m1. 349 s
User 0m1. 028 s
Sys 0m0. 316 S

Change to Char C [1024], that is, read 1024 bytes each time as a group, read 1 group, reduce the number of fread calls.

Num = fread (C, 1024, 1, FP)

Real 0m0. 041 s
User 0m0. 004 s
Sys 0m0. 036 s

Note that the returned value is the number of successfully read groups, not the number of bytes read.

If the number of bytes in the file is less than 1024, The fread above returns 0.

According to HuffmanProgramShould be a byte Group, read more groups at a time

The returned value of fread (C, 1, 1024, FP) is equivalent to the number of bytes read.

Name

Fread, fwrite-binary stream input/output

Synopsis

# Include <stdio. h>

Size_t fread (void * PTR, size_t size, size_t nmemb, file * stream );

Size_t fwrite (const void * PTR, size_t size, size_t nmemb,

File * stream );

The function fread () reads nmemb elements of data, each size bytes

Long, from the stream pointed to by stream, storing them at the loca-

Tion given by PTR.

Python, read ()
 
File = open ('HTTP: // www.cnblogs.com/big.log', 'rb ')While1: c = file. Read (1)IfC = '':Break# Print C,

Running time

Real 0m15. 013 s
User 0m13. 505 s

Python takes too much time to call functions than C.

 

C ++ istreambuf_iterator
# Include <iostream> # include <iterator> # include <String> # Include <fstream>Using NamespaceSTD;IntMain () {ifstream input_file ("Http://www.cnblogs.com/big.log", IOS: Binary );CharC;// --- Istreambuf_iterator read, will read directly from the input streamIstreambuf_iterator <Char> EOS;// End-of-range iteratorIstreambuf_iterator <Char> IIT (input_file );While(IIT! = EOS) C = * IIT ++;Return0 ;}

Real 0m3. 414 s
User 0m2. 528 s
Sys 0m0. 884 s

 

C ++ istream_iterator
# Include <iostream> # include <iterator> # include < String > # Include <fstream> // Show C ++ read a file by Bytes  // Show C read a file by Bytes  // Show System C read  // ================================================ ======================================  Using   Namespace STD; Int Main () {ifstream input_file (" Http://www.cnblogs.com/big.log ", IOS: Binary); input_file.unsetf (IOs: skipws); // a space character is required.Char C; // --- Istreambuf_iterator read, will read directly from the input stream Istream_iterator < Char > EOS; // End-of-range iterator Istream_iterator < Char > IIT (input_file ); While (IIT! = EOS) {c = * IIT ++; // Cout <C; } Return 0 ;}

Real 0m2. 433 s
User 0m1. 732 s
Sys 0m0. 704 s

All of the above are the results of GCC 4.2.4. It is strange that, according to article 29 of valid STL, istream_iterator is used to use operator >>while istreambuf_iterator is used to directly read data from the stream buffer, so istreambuf_iterator will.

However, my experiment results are slower.

If the speed of reading and writing files is emphasized, use C directly.

 

// C ++ cin. Get (CH). The returned value is the istream object of the application.
 
IntMain () {ifstream input_file ("Http://www.cnblogs.com/big.log", IOS: Binary );CharC [1024];// Input_file.get (C, 1024 );While(Input_file.Get(C [0]);// Cout <C [0];Return0 ;}

Real 0m2. 300 s
User 0m0. 472 s
Sys 0m1. 700 s

// C ++ cin. Get (), the returned value is an int,-1 indicates the end of EOF
 While(C [0] = input_file.Get())! = EOF) cout <C [0];

Real 0m0. 885 s
User 0m0. 524 S
Sys 0m0. 364 s

 

// Note that when you use get (char *, streamsize, delemeter = '\ n'), if you read 2 bytes, the second byte is \ 0, and input_file.gcount is 1.

Therefore, to use this function to read a character at a time, use 2 instead of 1.

 
Input_file.Get(C, 2, EOF );
 
 

 

// System C read ()
 # include 
   
    int  main () {
    // char Buf [2048];  
    char  C [1024]; 
    int  num; STD: 
    string  S =" 
    http://www.cnblogs.com/big.log  "; 
    // STD: String S =" http://www.cnblogs.com/test.log ";  
    // file * fp = fopen (S. c_str (), "rb");  
    int  fp = open (S. c_str (), o_rdonly); 
    while  (1) {
    // num = fread (C, 1024, 1, FP);  num = read (FP, C, 1 ); 
    // printf ("% C", C [0]);  
    If  (num = 0) 
    // If (C [0] = EOF)  
    Break ;}< span style = "color: # 0000ff"> return  0; 
  

real 0m43. 560 S
User 0m4. 908 S
sys 0m38. 594 S

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.