2

我可以使用 C 代码和/或系统程序,例如 time 来测量 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 库没有缓冲,操作系统也可能正在缓冲。

Also, since you are writing the data to the standard output, the bottleneck might be precisely there. Printing characters to a "console" (or "terminal") window in a graphic environment is surprisingly slow. Try writing to disc, or nowhere at all. You will probably get different results.

于 2013-03-08T05:42:56.550 回答