Efficiency Comparison Between fstream and C-Style File Read/Write methods (such as fread and fwrite)

Source: Internet
Author: User

 

In order to explore the efficiency of reading and writing files in the C ++ style fstream and C style (such as fread and fwrite), I made two special experiments.

 

My machine is Windows XP, Visual Studio 2008

 

1. Test File writing speed

 

 

Program Design Idea: Write test_size characters into files in two ways and record the time consumption in two ways.

 

 

Lab code:

 

Void test_write () <br/>{< br/> const int test_size = 10000000; <br/> const char * c_plus_write_file = "H: // c_plus_write_file.txt "; <br/> const char * c_write_file = "H: // c_write_file.txt"; </P> <p> cout <"test size:" <test_size <Endl; <br/> // C ++ style Writing File <br/> ofstream of (c_plus_write_file); <br/> assert (of); <br/> time_t start, end; <br/> Start = clock (); <br/> for (INT I = 0; I <test_size; ++ I) <br/>{< br/> char TMP [1]; <br/> TMP [0] = char (I ); <br/> of <TMP [0]; <br/>}< br/> end = clock (); <br/>. close (); <br/> cout <"C ++ style:" <End-start <"Ms" <Endl; <br/> // C style Writing File <br/> file * fp = fopen (c_write_file, "W"); <br/> Start = clock (); <br/> for (INT I = 0; I <test_size; ++ I) <br/>{< br/> char TMP [1]; <br/> TMP [0] = char (I); <br/> fwrite (TMP, 1, 1, FP ); <br/>}< br/> end = clock (); <br/> fclose (FP); <br/> cout <"C style: "<End-start <" Ms "<Endl; <br/> cin. get (); <br/>}< br/> 

 

Experiment results:

Figure 1

Figure 2

Figure 3

 

** As shown in figures 1, 2, and 3, the <operator of ofstream consumes nearly three times the time of fwrite ().

 

 

Changed the code of <to: Of. Write (TMP, 1 );
Result:

 

 

Lab code:

 

Void test_write () <br/>{< br/> const int test_size = 1000000; <br/> const char * c_plus_write_file = "H: // c_plus_write_file.txt "; <br/> const char * c_write_file = "H: // c_write_file.txt"; </P> <p> cout <"test size:" <test_size <Endl; <br/> // C ++ style Writing File <br/> ofstream of (c_plus_write_file); <br/> assert (of); <br/> time_t start, end; <br/> Start = clock (); <br/> for (INT I = 0; I <test_size; ++ I) <br/>{< br/> char TMP [1]; <br/> TMP [0] = char (I); <br/>. write (TMP, 1); <br/>}< br/> end = clock (); <br/>. close (); <br/> cout <"C ++ style:" <End-start <"Ms" <Endl; <br/> // C style Writing File <br/> file * fp = fopen (c_write_file, "W"); <br/> Start = clock (); <br/> for (INT I = 0; I <test_size; ++ I) <br/>{< br/> char TMP [1]; <br/> TMP [0] = char (I); <br/> fwrite (TMP, 1, 1, FP ); <br/>}< br/> end = clock (); <br/> fclose (FP); <br/> cout <"C style: "<End-start <" Ms "<Endl; <br/> cin. get (); <br/>}< br/> 

Experiment results:

 

Figure 4

Figure 5

Figure 6

 

 

Comparison between figure 4 and figure 1, Figure 5 and figure 2, figure 6 and Figure 3 shows that the <operator does not have ofstream. write (), fast, but the two are still not fwrite () fast

 

Conclusion: Efficiency fwrite ()> ofstream. Operator <()> ofstream. Write ()

 

 

 

 

3. The following is a comparison of read files:

 

Program Design Idea: Use two methods to read a text of nearly MB and record the time.

 

Lab code:

Void test_read () <br/>{< br/> const char * read_file = "H: // read4.txt"; <br/> const int buf_size = 1024; <br/> char Buf [buf_size]; <br/> // C ++ style Writing File <br/> ifstream ifs (read_file, IOS: Binary ); <br/> assert (IFS); <br/> time_t start, end; <br/> Start = clock (); <br/> while (! IFS. EOF () <br/>{< br/> ifs. read (BUF, buf_size); <br/>}< br/> end = clock (); <br/> ifs. close (); <br/> cout <"C ++ style:" <End-start <"Ms" <Endl; <br/> // C style Writing File <br/> file * fp = fopen (read_file, "rb"); <br/> Start = clock (); <br/> int Len = 0; <br/> DO <br/> {<br/> Len = fread (BUF, 1, buf_size, FP ); <br/> // cout <Len <Endl; <br/>} while (Len! = 0); <br/> end = clock (); <br/> fclose (FP); <br/> cout <"C style: "<End-start <" Ms "<Endl; <br/> cin. get (); <br/>}< br/> 

 

Experiment results:

Figure 7

 

Conclusion: to read a 100 M file, fread () is nearly ten times more efficient than ifstream. Read! (This conclusion is amazing !)

 

 

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.