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.
|