如果我计算这两个功能,等待有什么区别?

时间:2013-03-08 05:29:35

标签: c algorithm time buffer

我可以使用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);
    }
}

1 个答案:

答案 0 :(得分:2)

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

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