2

C コードおよび/またはシステム プログラム (時間など) を使用して 2 つの関数の違いを測定できますが、何が期待できますか? 比率が 1:1024 であるからといって、バッファリングが 1024 倍高速であることを意味するわけではありませんか?

#define SIZE 1024 /* read 1024 bytes at a time */

int main(int argc, char **argv)
{
    void info(char file_name[]);
    void buffered(char file_name[]);

    info("/proc/scsi/scsi"); /* read a byte at a time */
    buffered("/proc/cpuinfo"); /* read 1024 bytes at a time */
    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);
    }
}

どのくらいバッファする必要がありますか?

アップデート

違いが大きいというタイマーを追加しました。

$ cc cpu-disk-info.c
dev@dev-OptiPlex-745:~$ ./a.out 
Unbuffered: 0.040000 seconds
Buffered: 0.000000 seconds

コード

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 1024 /* read 1024 bytes at a time */

int main(int argc, char **argv)
{
    void info(char file_name[]);
    void buffered(char file_name[]);
    clock_t toc;
    clock_t tic = clock();
    info("Cube.001.skeleton.xml"); /* read a byte at a time */
    toc = clock();
    printf("Unbuffered: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
    tic = clock();    
    buffered("Cube.001.skeleton.xml"); /* read 1024 bytes at a time */
    toc = clock();
    printf("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);
    }
}

テスト 2

このテストによると、バッファリングされた i/o は、バッファリングされていない i/o よりも 19 倍高速 (?) です。

Unbuffered: 0.190000 seconds
Buffered: 0.010000 seconds

ソース

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 1024 /* read 1024 bytes at a time */

int main(int argc, char **argv)
{
    void info(char file_name[]);
    void buffered(char file_name[]);
    clock_t toc;
    clock_t tic = clock();
    info("Cube.001.skeleton.xml"); /* read a byte at a time */
    info("Cube.001.skeleton.xml"); /* read a byte at a time */
    info("Cube.001.skeleton.xml"); /* read a byte at a time */
    info("Cube.001.skeleton.xml"); /* read a byte at a time */
    info("Cube.001.skeleton.xml"); /* read a byte at a time */
    toc = clock();
    printf("Unbuffered: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
    tic = clock();    
    buffered("Cube.001.skeleton.xml"); /* read 1024 bytes at a time */
    buffered("Cube.001.skeleton.xml"); /* read 1024 bytes at a time */
    buffered("Cube.001.skeleton.xml"); /* read 1024 bytes at a time */
    buffered("Cube.001.skeleton.xml"); /* read 1024 bytes at a time */
    buffered("Cube.001.skeleton.xml"); /* read 1024 bytes at a time */
    toc = clock();
    printf("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);
    }
}
4

1 に答える 1

2

多少の違いはあるかもしれませんが、1024の係数ではありません。info()関数では、バッファリングは行っていませんが、I/Oライブラリはバッファリングしています。I / Oライブラリがバッファリングされていなくても、オペレーティングシステムはおそらくバッファリングしています。

また、データを標準出力に書き込んでいるため、ボトルネックが正確に存在する可能性があります。グラフィック環境の「コンソール」(または「ターミナル」)ウィンドウへの文字の印刷は、驚くほど遅いです。ディスクに書き込んでみてください。どこにも書いてはいけません。おそらく異なる結果が得られます。

于 2013-03-08T05:42:56.550 に答える