【问题标题】:What difference is awaited if I time these 2 functions?如果我对这两个功能计时,有什么区别?
【发布时间】:2013-02-23 14:31:28
【问题描述】:

我可以使用 C 代码和/或系统程序,例如是时候测量这两个函数的差异了,但我们会期待什么呢?仅仅因为比率是 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);
    }
}

【问题讨论】:

  • 你给他们计时了吗?我总是对我们的预期与实际发生的事情感兴趣。
  • @DougRamsey 我添加了一个说明差异很大的时间。感谢您的评论。我没有理论分析,但在 C 代码中进行了测量。

标签: c algorithm time buffer


【解决方案1】:

可能会有一些差异,但不是 1024 倍。在您的 info() 函数中,您不是在缓冲,但 I/O 库是。即使 I/O 库没有缓冲,操作系统也可能正在缓冲。

此外,由于您正在将数据写入标准输出,因此瓶颈可能就在那里。在图形环境中将字符打印到“控制台”(或“终端”)窗口非常慢。尝试写入光盘,或者根本不写入。您可能会得到不同的结果。

【讨论】:

  • 感谢您的回答。我添加了一个测试,表明差异很大,但没有说明因素。在测试中,我将实际写入标准输出的内容注释掉了。
  • 哦,你可能想用这两个函数打开同一个文件 ;-)
  • 在测试中,我让两个函数处理相同的数据大小。我这样做了 5 次,以便缓冲 i/o 将获得一个 > 0 的值,并且确实如此。根据测试,它说缓冲 i/o 比无缓冲快 19 倍。
猜你喜欢
  • 2011-01-10
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 2013-07-14
相关资源
最近更新 更多