fseek速度 [英] fseek speed

查看:233
本文介绍了fseek速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我很困惑......我正在努力提高程序的速度

我写的是执行随机访问在一个文件中。它b / b
很大程度上依赖于fseek并且非常慢。为了测试,我写了下面的

测试程序,它只是将数字1-167721顺序写入

a二进制文件:


#include< stdio.h>

#include< stdlib.h>

#include< sys / types.h>

#include< sys / stat.h>

#include< fcntl.h>


静态长MAXNUM = 16777214;


int main(){

FILE * fp;

long i,tmp;

int j;

unsigned char c;


fp = NULL;

if((fp = fopen(" test.out"," w) + b"))== NULL){

fprintf(stderr,Failed.\ n);

退出(22);

}


fprintf(stderr,Writing ...);

for(i = 0; i< MAXNUM / 10 ; i ++){

// fseek(fp,0,SEEK_CUR);

tmp = i;

for(j = 0; j < 3; j ++){

c =(unsigned char)(tmp%256);

tmp / = 256;

fwrite(& ; c,1, 1,fp);

}

}

fprintf(stderr,done.\ n);


fclose(fp);


返回0;

}

编译并运行时linux-2.4。##系统和一个旧的DOS系统它

非常快。现在,如果你取消注释说fseek(fp,0,

SEEK_CUR)的行,它会慢17倍!


无论如何要提高速度打电话给

fseek?

提前感谢任何想法,

TJ Walls

博士候选人 - 石溪大学物理系

Hello All,

I am baffled ... I am trying to improve the speed of a program
that I have written that performs random access within a file. It
relies heavily on fseek and is very slow. To test, I wrote the following
test program which just writes the numbers 1-167721 sequentially to
a binary file:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static long MAXNUM = 16777214;

int main() {
FILE *fp;
long i, tmp;
int j;
unsigned char c;

fp = NULL;
if ((fp = fopen("test.out", "w+b")) == NULL) {
fprintf(stderr, "Failed.\n");
exit(22);
}

fprintf(stderr, "Writing ... ");
for (i = 0; i < MAXNUM/10; i++) {
//fseek(fp, 0, SEEK_CUR);
tmp = i;
for (j = 0; j < 3; j++) {
c = (unsigned char)(tmp % 256);
tmp /= 256;
fwrite(&c, 1, 1, fp);
}
}
fprintf(stderr, "done.\n");

fclose(fp);

return 0;
}
When compiled and run on a linux-2.4.## system and an old DOS system it
is very fast. Now if you uncomment the line that says fseek(fp, 0,
SEEK_CUR), it runs 17x slower!

Is there anyway to improve on the speed hit incurred by the call to
fseek?

Thanks in advance for any thoughts,
TJ Walls
Ph.D. Candidate - Physics Dept. Stony Brook University

推荐答案



哎呀...当然我的意思是''写下数字0-167720''...

Ooops ... of course I meant ''writes the numbers 0-167720'' ...


TJ Walls写道:

[...]
TJ Walls wrote:
[...]
for( i = 0; i< MAXNUM / 10; i ++){
// fseek(fp,0,SEEK_CUR);
tmp = i;
for(j = 0; j< 3; j ++){
c =(unsigned char)(tmp%256);
tmp / = 256;
fwrite(& c,1,1,fp);
}
}
[...]当在linux-2.4。##系统和旧的DOS系统上编译和运行时,它非常快。现在,如果你取消注释说fseek(fp,0,
SEEK_CUR)的行,它的运行速度会慢17倍!

无论如何都要提高对 fseek?
for (i = 0; i < MAXNUM/10; i++) {
//fseek(fp, 0, SEEK_CUR);
tmp = i;
for (j = 0; j < 3; j++) {
c = (unsigned char)(tmp % 256);
tmp /= 256;
fwrite(&c, 1, 1, fp);
}
} [...] When compiled and run on a linux-2.4.## system and an old DOS system it
is very fast. Now if you uncomment the line that says fseek(fp, 0,
SEEK_CUR), it runs 17x slower!

Is there anyway to improve on the speed hit incurred by the call to
fseek?




你拥有的fseek()调用本质上是一个无操作。你很想在当前位置获得



然而,fseek的副作用是刷新缓冲区。没有
fseek(),你的输出(实际上,我认为可以在

一般意义上是正确的)被缓冲,并且只在缓冲区写入并祝。使用
fseek(),你强制为每个角色写缓冲区。


你在这里使用fseek()的目的是什么? br />

-

+ ------------------------- + - ------------------ + ----------------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody at spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +



The fseek() call you have is, in essence, a no-op. You are fseek''ing to
the current location.

However, a side-effect of the fseek is the flushing of the buffer. Without
the fseek(), your output will (actually, I suppose "can" is correct in the
general sense) be buffered, and only written when the buffer fille. With
the fseek(), you are forcing the buffer to be written for every character.

What is your purpose for using fseek() here?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+


你在这里使用fseek()的目的是什么?
What is your purpose for using fseek() here?




我把fseek()的目的放在这里是测试

fseek()给了我什么样的时间(在这种情况下,我认为

也应该是无操作...因此给我一个0的时间罚款,但在

的事实上,成本是巨大的)。在我的真实程序中,我正在寻找文件中的各种

位置并读取3个字节的数据,所以fseek有一个目的

那里,但它运行得非常慢,我想找出一种方法来增加它的价格。

-TJ Walls



My purpose for putting the fseek() here is to test
what kind of time hit fseek() is giving me (in this case, I thought it
should be a no-op too ... and thus give me a time penalty of 0, but in
fact the cost is HUGE). In my real program I am fseeking to various
places in the file and reading 3 bytes of data, so the fseek has a purpose
there, but it runs very slowly and I am trying to figure out a way to
speed it up.
-TJ Walls


这篇关于fseek速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆