Standard C IO function and kernel IO function efficiency (time) comparison

Source: Internet
Author: User

  

Objective

Standard C provides file-related IO functions, except that the standard error output is unbuffered (the error message can be displayed as soon as possible), all terminal-related are row buffers, and the rest are fully buffered.

We can use SETBUF,SETVBUF to change the buffer type of the specified stream.

Prototype:

   void Char *buf); int Char int mode, size_t size); Successful return 0, failure not 0

The function is used at a glance, and when the parameter BUF is specified as NULL, the operating system automatically allocates the appropriate buffer for the given stream. The length is Bufsiz (no e, defined in stdio.h).

The function of buffering can reduce the number of system calls, the system call needs the overhead, and the operation of the operating system is required from the user state to the kernel state switch. The appropriate length of buffer size is important for enhancing IO efficiency.

Comparison of kernel functions and standard C functions

File size processed: 270M of text

Comparison of file copy run times
function Sys User Real Note
Fgetc,fputc 0m0.848s 0m6.232s 0m7.614s
Fgets,fputs Fastest Speed 0m1.204s 0m0.800s 0m3.709s
Read,write Fastest Speed 0m3.380s 0m0.752s 0m0.008s BUF[1024*8]
Read,write Buffer ==1 18m49.875s 0m35.166s 19m33.293s

Comparative conclusion:

1, the standard IO library function and the kernel function is not different how much, it is not particularly necessary in the case of standard IO is not a good choice.

2, Fgets (fputs) than fgetc (FPUTC) twice times faster, visible fgets (fputs) is not implemented with FGETC (FPUTC), otherwise their efficiency should be similar, according to the UC advanced programming that book, Fgets, Fputs is implemented with memccpy, and memccpy is written by sinks, which is naturally more efficient than the C function.

Attached memccpy prototype

    

void *memcpy (voidconstvoid *src, size_t n); // Copy no more than n bytes to dest from SRC, and the N character value is encountered void *memccpy (voidconstvoidint C, size_t N);

Standard C IO function and kernel IO function efficiency (time) comparison

Related Article

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.