Standard I/O efficiency (5.8)

Source: Internet
Author: User
Document directory
  • 5.8. Standard I/O efficiency
5.8. Standard I/O efficiency

Using the functions from the previous section, we can get an idea of the efficiency of the standard I/O system. the program in Figure 5.4 is like the one in Figure 3.4: it simply copies standard input to standard output, usingGETCAndPutc. These two routines can be implemented as macros.

Figure 5.4. Copy standard input to standard output using GETCAnd Putc
# Include "apue. H"
Int main (void ){
Int C;
While (C = GETC (stdin ))! = EOF)
If (putc (C, stdout) = EOF)
Err_sys ("output error ");
If (ferror (stdin ))
Err_sys ("input error ");
Exit (0 );
}
We can make another version of this program that uses fgetc and fputc, which should be functions, not macros. (We don't show this trivial change to the source code.)

Finally, we have a version that reads and writes lines, shown in Figure 5.5.

Figure 5.5. Copy standard input to standard output using FgetsAnd Fputs
# Include "apue. H"
Int main (void ){
Char Buf [maxline];
While (fgets (BUF, maxline, stdin )! = NULL)
If (fputs (BUF, stdout) = EOF)
Err_sys ("output error ");
If (ferror (stdin ))
Err_sys ("input error ");
Exit (0 );
}
Note that we do not close the standard I/O streams explicitly in Figure 5.4 or Figure 5.5. Instead, we know that the exit function will flush any unwritten data and then close all open streams. (We'll discuss this in Section 8.5.) It is interesting to compare the timing of these three programs with the timing data from Figure 3.5. We show this data when operating on the same file (98.5 MB with 3 million lines) in Figure 5.6.

Figure 5.6. Timing results using standard I/O routines

Function

User CPU (seconds)

System CPU (seconds)

Clock Time (seconds)

Bytes of program text

Best time from Fig 3.5

0.01

0.18

6.67

 

Fgets,Fputs

2.59

0.19

7.15

139

GETC,Putc

10.84

0.27

12.07

120

Fgetc,Fputc

10.44

0.27

11.42

120

Single byte time from Fig 3.5

124.89

161.65

288.64

 

 

For each of the three standard I/O versions, the user CPU time is larger than the bestReadVersion from Figure 3.5, because the character-at-a-time standard I/O versions have a loop that is executed 100 million times, and the loop in the line-at-a-time version is executed 3,144,984 times. inReadVersion, its loop is executed only 12,611 times (for a buffer size of 8,192 ). this difference in clock times is from the difference in user times and the difference in the times spent waiting for I/O to complete, as the system times are comparable.

The system CPU time is about the same as before, because roughly the same number of kernel requests are being made. note that An advantage of using the standard I/O routines is that we don't have to worry about buffering or choosing the optimal I/O size. we do have to determine the maximum line size for the version that usesFgets, But that's easier than trying to choose the optimal I/O size.

The final column in Figure 5.6 is the number of bytes of text spacethe machine instructions generated by the C compilerfor each ofMainFunctions. We can see that the version usingGETCAndPutcTakes the same amount of space as the one usingFgetcAndFputcFunctions. Usually,GETCAndPutcAre implemented as macros, but in the gnu c library implementation, the macro simply expands to a function call.

The version using line-at-a-time I/O is almost twice as fast as the version using character-at-a-time I/O. IfFgetsAndFputsFunctions are implemented usingGETCAndPutc(See section 7.7 of kernighan and Ritchie [1988], for example), then we wowould perform CT the timing to be similar toGETCVersion. actually, we might should CT the line-at-a-time version to take longer, since we wocould be adding the overhead of 200 million extra function CALS to the existing 6 million ones. what is happening with this example is that the line-at-a-time functions are implemented usingMemccpy(3). Often,MemccpyFunction is implemented in ER er instead of C, for efficiency.

The last point of interest with these timing numbers is thatFgetcVersion is so much faster thanBuffsize = 1Version from Figure 3.5. Both involve the same number of function callsabout 200 millionyetFgetcVersion is almost 12 times faster in user CPU time and slightly more than 25 times faster in clock time. The difference is that the version usingReadExecutes 200 million function CILS, which in turn execute 200 million system CILS. WithFgetcVersion, we still execute 200 million function cals, but this ends up being only 25,222 system cals. System CILS are usually much more expensive than ordinary function CILS.

As a disclaimer, you shocould be aware that these timing results are valid only on the single system they were run on. the results depend on your implementation features that aren't the same on every UNIX system. nevertheless, having a set of numbers such as these, and explaining why the varous Versions differ, helps us understand the system better. from this section and section 3.9, we 've learned that the standard I/O library is not much slower than callingReadAndWriteFunctions directly. The approximate cost that we 've seen is about 0.11 seconds of CPU time to copy a megabyte of data usingGETCAndPutc. For most nontrivial applications, the largest amount of the user CPU time is taken by the application, not by the standard I/O routines.

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.