The difference between standard C library functions and system libraries under Linux (i.e., learning with no buffers)

Source: Internet
Author: User

As we all know, C language in the Unix/linux system has a set of system calls (System functions), such as file Operation Open (), close (), write (), read () and so on, while the standard C language library function has a set of file Operation function fopen (), Fclose (), fwrite (), fread (), etc... So the same is the operation of the file function, labeled C and UC What is the difference? is the standard C high efficiency or UC efficient? Let's find out today.

Program function: Writes the 0~999999 1 million integer data to the file.

1, standard C to achieve a large number of data write files:
/* File name test1.c*/#include <stdio.h>#include <stdlib.h>#define MAX 1000000int main (void) {FILE * fp = fopen ("Test1.txt","w"); if (fp = = NULL) {perror ("fopen test1.txt"); exit (exit_failure);} int i; For (i = 0; i < MAX; i++) {fwrite (&i, sizeof (int), 1, FP);  /* We use fwrite one write file */} fclose (FP); return 0;} /*gcc-o test1 test1.c compilation *                / 

Efficiency test results for writing files:

2, Linux system functions to achieve a large number of data write files:
/* File name test2.c*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h> #define MAX 1000000int Main ( void) {int fd = open ( "Test2.txt", o_rdwr| o_trunc| O_creat,0666); /*o_rdwr readable writable, o_creat no new, O_trunc overwrite write (original content small empty), O_append append write (original content not empty) */if (fd = =-1) {perror (" open test2.txt "); exit (exit_failure); } int i; for (i = 0; i < MAX; i++) {write (FD, &i, sizeof (int)); } close (FD); return 0;} /*gcc-o test2 test2.c Compilation */         

Write Efficiency test results:

We found that the use of system functions did not make the program run at a rate (write rate) faster but was dozens of times times slower, and in the test we can see that the increased time is basically increased in the SYS (System level). This is because the system functions do not have buffers at the user level, there are buffers at the kernel level, but the buffers are small, so a large amount of data is written to the file, and the write rate is reduced by frequent flush buffers. Therefore, system functions (such as write) need to add custom buffers to increase the rate, while standard C functions (such as fwrite) do not have to set their own buffers, if the system functions with custom buffers its efficiency is better than subscript C, do not define the buffer is inefficient, but the buffer size is appropriate, Otherwise extremes meet (even if there is a buffer, writing from the buffer to the file is also one write).

3, Linux system functions to achieve a large number of data write file modification:
/* File name test3.c*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#define MAX 1000000#define BUF_SIZE 500/* Buffer size, appropriate, how much for the appropriate needs of their own testing, but different size of the buffer effect is certainly different */int main (void) {int fd = open ( "Test3.txt", o_rdwr| o_trunc| O_creat,0666); if (fd =-1) {perror ( "open Test3.txt "); exit (exit_failure);} int i; int buffer[buf_size] = {0}; for (i = 0; i < MAX; i++) {buffer[i%buf_size] = i; if (i%buf_size = = Buf_size-1) write (fd, buffer,  sizeof (buffer)); /* writes to the file once the buffer is full (flushes the buffer once) */} close (FD); return 0;} /*gcc-o test3 test3.c Compilation */         

Write Efficiency test results:

After adding a custom buffer, the test found that its efficiency was 100 times faster than the non-self-defined buffer, and that the write rate of the marked C was several multiples faster. If the buffer size is appropriate, its efficiency will increase even more. So when we change Buf_szie to a larger value (buf_szie=50000), after recompiling, the test results are as follows:

This efficiency is improved, so choosing a suitable buffer is especially important.

The difference between standard C library functions and system libraries under Linux (i.e., learning with no buffers)

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.