由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 有没有现成的模拟fread的bufferRead()?
相关主题
serialization 到底该怎么理解啊?c++如何把小数转成二进制输出到文本文件?
fread/fwrite有big/small endian问题吗?size of structure
ask a question about struct in C programmingJava question
python question, easy onesize不固定的struct怎么定义呀?
请教一个结构体占内存大小的问题sizeof()的问题
问一道有关C++ (de)serialization的问题,谢谢!C: struct 里面一个CHAR, 一个INT, 这个STRUCT 占多少字节?
写惯了C++ code,再写C code真不习惯初学C,对什么该free一直搞不明白
ask a simple question about int pointer.一个windows编程问题
相关话题的讨论汇总
话题: buffer话题: fread话题: bufferread话题: sizeof话题: int
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
在做IO优化。原来有大量fread操作,现在想精简成一次读到一个buffer里,从里面取
出data。有没有这样现成的库函数啊?
C++的binary stream应该可以把?但上次有个讨论说C++的binary stream比较慢?
原来的code:
for (int i = 0; i < count; i++) {
int x;
fread(&x, sizeof(int), 1, input_fp);
long y;
fread(&y, sizeof(long), 1, input_fp);
}
现在想改成:
void** buffer = new char[count];
fread(*buffer, sizeof(char), count, input_fp);
for (int i = 0; i < count; i++) {
int x;
bufferRead(&x, sizeof(int), 1, buffer);
long y;
bufferRead(&y, sizeof(long), 1, buffer)
p***o
发帖数: 1252
2
fread本来就是带buffer的把。

【在 g*********s 的大作中提到】
: 在做IO优化。原来有大量fread操作,现在想精简成一次读到一个buffer里,从里面取
: 出data。有没有这样现成的库函数啊?
: C++的binary stream应该可以把?但上次有个讨论说C++的binary stream比较慢?
: 原来的code:
: for (int i = 0; i < count; i++) {
: int x;
: fread(&x, sizeof(int), 1, input_fp);
: long y;
: fread(&y, sizeof(long), 1, input_fp);
: }

g*********s
发帖数: 1782
3
但是如果count是个很大的数字,fread会call很多次。
当然,我现在还不清楚这样是不是能省下开销来。想试试。

【在 p***o 的大作中提到】
: fread本来就是带buffer的把。
p***o
发帖数: 1252
4
你至少要profile一下吧。

【在 g*********s 的大作中提到】
: 但是如果count是个很大的数字,fread会call很多次。
: 当然,我现在还不清楚这样是不是能省下开销来。想试试。

g*********s
发帖数: 1782
5
profile过了。看到fread开销不小才想这么做的。
也许是必须的开销,但不试试怎么知道。

【在 p***o 的大作中提到】
: 你至少要profile一下吧。
c*****t
发帖数: 1879
6
先把整个文件读进一个大的 char* buffer 。然后
for (...)
{
// for portable code do... change shift for different endian
x = ((buffer[i] & 0xff) << 24) |
((buffer[i + 1] & 0xff) << 16) |
((buffer[i + 2] & 0xff) << 8) |
(buffer[i + 3] & 0xff);
// otherwise can use the following
// occasioanlly may need to consider alignment
x = *(int*)&buffer[i];
i += 4;
// likewise for long etc
...
}

【在 g*********s 的大作中提到】
: 在做IO优化。原来有大量fread操作,现在想精简成一次读到一个buffer里,从里面取
: 出data。有没有这样现成的库函数啊?
: C++的binary stream应该可以把?但上次有个讨论说C++的binary stream比较慢?
: 原来的code:
: for (int i = 0; i < count; i++) {
: int x;
: fread(&x, sizeof(int), 1, input_fp);
: long y;
: fread(&y, sizeof(long), 1, input_fp);
: }

b******n
发帖数: 592
7
why not use struct, and read everything in one go. Although you may want to
turn off struct padding when you compile your code.

【在 g*********s 的大作中提到】
: profile过了。看到fread开销不小才想这么做的。
: 也许是必须的开销,但不试试怎么知道。

g*********s
发帖数: 1782
8
This is a simplified example for demo. In reality it's a variable-length
record read-in in the loop. So struct doesn't work w/o the regularity.

to

【在 b******n 的大作中提到】
: why not use struct, and read everything in one go. Although you may want to
: turn off struct padding when you compile your code.

b******n
发帖数: 592
9
Then use simple 8K blocks.

【在 g*********s 的大作中提到】
: This is a simplified example for demo. In reality it's a variable-length
: record read-in in the loop. So struct doesn't work w/o the regularity.
:
: to

b******n
发帖数: 592
10
Another solution to reduce I/O cost is to use compression library such as
zlib.

【在 g*********s 的大作中提到】
: This is a simplified example for demo. In reality it's a variable-length
: record read-in in the loop. So struct doesn't work w/o the regularity.
:
: to

g*********s
发帖数: 1782
11
I did a test and posted the result just yesterday.
Basically, if you use customized buffer, you can save time by another 10%
than default fread buffer.

【在 b******n 的大作中提到】
: Then use simple 8K blocks.
b******n
发帖数: 592
12
My program are always computation expensive. Loading data is never a problem
. It is slow, but normally we don't care. As I said, using compression libra
ry will reduce I/O as well.

【在 g*********s 的大作中提到】
: I did a test and posted the result just yesterday.
: Basically, if you use customized buffer, you can save time by another 10%
: than default fread buffer.

g*********s
发帖数: 1782
13
It's binary data. zip not help much.

problem
libra

【在 b******n 的大作中提到】
: My program are always computation expensive. Loading data is never a problem
: . It is slow, but normally we don't care. As I said, using compression libra
: ry will reduce I/O as well.

1 (共1页)
进入Programming版参与讨论
相关主题
一个windows编程问题请教一个结构体占内存大小的问题
请教一个C的问题问一道有关C++ (de)serialization的问题,谢谢!
大家新年好。 请教一个 c interview question写惯了C++ code,再写C code真不习惯
a careercup questionask a simple question about int pointer.
serialization 到底该怎么理解啊?c++如何把小数转成二进制输出到文本文件?
fread/fwrite有big/small endian问题吗?size of structure
ask a question about struct in C programmingJava question
python question, easy onesize不固定的struct怎么定义呀?
相关话题的讨论汇总
话题: buffer话题: fread话题: bufferread话题: sizeof话题: int