gpt4 book ai didi

c - 如果我为这两个功能计时,等待有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:52:05 26 4
gpt4 key购买 nike

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

最佳答案

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

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

关于c - 如果我为这两个功能计时,等待有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15287349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com