buffered 0.030000 seconds が「より良い」 buffered 0.030000 seconds と同じなのはなぜですか? ラインサイズを 4 倍にしても時間が変わらない場合、どうすればさらに高速化できますか?
テスト
$ ./a.out
Unbuffered: 0.770000 seconds
Buffered: 0.030000 seconds
Better buffered: 0.030000 seconds
コード
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 1024 /* read 1024 bytes at a time */
#define betterSIZE 4096 /* read a better size at a time */
int copy() /* input2output ie anything to anything */
{
char buf[betterSIZE];
int n;
while ((n = read(0, buf, betterSIZE)) > 0)
write(1, buf, n);
return 0;
}
int main(int argc, char **argv)
{
/* copy(); */
/* make the names known */
void info(char file_name[]);
void buffered(char file_name[]);
void better_buffered(char file_name[]);
/* test */
clock_t toc;
clock_t tic = clock();
info("coreutils_8.13.orig.tar.gz");
info("coreutils_8.13.orig.tar.gz");
info("coreutils_8.13.orig.tar.gz");
info("coreutils_8.13.orig.tar.gz");
info("coreutils_8.13.orig.tar.gz");
toc = clock();
printf("Unbuffered: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
tic = clock();
buffered("coreutils_8.13.orig.tar.gz");
buffered("coreutils_8.13.orig.tar.gz");
buffered("coreutils_8.13.orig.tar.gz");
buffered("coreutils_8.13.orig.tar.gz");
buffered("coreutils_8.13.orig.tar.gz");
toc = clock();
printf("Buffered: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
tic = clock();
better_buffered("coreutils_8.13.orig.tar.gz");
better_buffered("coreutils_8.13.orig.tar.gz");
better_buffered("coreutils_8.13.orig.tar.gz");
better_buffered("coreutils_8.13.orig.tar.gz");
better_buffered("coreutils_8.13.orig.tar.gz");
toc = clock();
printf("Better buffered: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
return 0;
}
void info(char file_name[])
{
int ch;
FILE *fp;
fp = fopen(file_name,"r");
// read mode
if (fp == NULL)
{
perror(file_name);
exit(EXIT_FAILURE);
}
while ((ch = fgetc(fp)) != EOF)
{
//putchar(ch);
}
fclose(fp);
}
void buffered(char file_name[])
{
char buf[SIZE];
FILE *fp;
size_t nread;
fp = fopen(file_name, "r");
if (fp) {
while ((nread = fread(buf, 1, sizeof buf, fp)) > 0)
{
//fwrite(buf, 1, nread, stdout);
}
if (ferror(fp)) {
/* to do: deal with error */
}
fclose(fp);
}
}
void better_buffered(char file_name[])
{
char buf[betterSIZE];
FILE *fp;
size_t nread;
fp = fopen(file_name, "r");
if (fp) {
while ((nread = fread(buf, 1, sizeof buf, fp)) > 0)
{
//fwrite(buf, 1, nread, stdout);
}
if (ferror(fp)) {
/* to do: deal with error */
}
fclose(fp);
}
}