Demo entry 6505785

copy1.c

   

Submitted by lyyang on May 30, 2017 at 18:12
Language: C. Code size: 2.2 kB.

#include <sys/stat.h>
#include <sys/fcntl.h>
#include <stdio.h>
#define BUF_1_MB 1024
#define BUF_1_B 1
#include <time.h>

main(int argc, char* argv[])
{
	clock_t flg1, flg2, flg3, flg4, flg5, flg6, flg7, flg8, flg9, flg10;
        int fd1,fd2;
	int n;
	char buf1[BUF_1_B],buf2[BUF_1_MB],buf3[BUF_1_B],buf4[BUF_1_MB];
	FILE *fp1,*fp2;
        
	
        flg1 = clock();
        /////////////////////////////////////////////////////////////
        fd1=open(argv[1],O_RDONLY);
	fd2=open(argv[2],O_WRONLY|O_CREAT,0644);
	//////////////////////////////////////////////////////////////
        flg2 = clock();
	while((n=read(fd1,buf1,BUF_1_B))>0)
	{
		write(fd2,buf1,n);
		}
        /////////////////////////////////////////////////////////////////
	flg3 = clock();

	n = 0;
        printf("read&write by 1B: %f\n",(double)(flg3-flg1)/CLOCKS_PER_SEC);

	flg4 = clock();
        ////////////////////////////////////////////////////////////////////
        while((n=read(fd1,buf2,BUF_1_MB))>0)
	{
		write(fd2,buf2,n);
		}
  	//////////////////////////////////////////////////////////////////////
        flg5 = clock();
        printf("read&write by 1MB: %f\n", (double)(flg5-flg4+flg2-flg1)/CLOCKS_PER_SEC);
		
        flg6 = clock();
 	////////////////////////////////////////////////////////////////////////
	fp1=fopen(argv[1],"r");
	fp2=fopen(argv[3],"w"); 
	///////////////////////////////////////////////////////////////////////
        flg7 = clock();
	//////////////////////////////////////////////////////////////////////////
	while((n=fread(buf3,BUF_1_B,1,fp1))>0)
	{
		fwrite(buf3,BUF_1_B,1,fp2);
		}
	////////////////////////////////////////////////////////////////////////////
	flg8 = clock();
        printf("fread&fwrite by 1B: %f\n", (double)(flg8-flg6)/CLOCKS_PER_SEC);
	////////////////////////////////////////////////////////////////////////////
	flg9 = clock();
	while((n=fread(buf4,BUF_1_MB,1024,fp1))>0)
	{
		fwrite(buf4,BUF_1_MB,1024,fp2);
		
	}
	////////////////////////////////////////////////////////////////////////////////
        flg10 = clock();
        printf("fread&fwrite by 1MB: %f\n", (double)(flg10-flg9+flg7-flg6)/CLOCKS_PER_SEC);


}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).