ifstream与fread的二进制文件(ifstream vs. fread for binary files)
哪个更快?
ifstream
或fread
。
我应该用哪个来读取二进制文件?
fread()
将整个文件放入内存中。
所以在fread
之后,访问它创建的缓冲区很快。
ifstream::open()
是否将整个文件放入内存?
或者每次运行ifstream::read()
时它是否访问硬盘?那么......
ifstream::open()
==fread()
吗?
或(ifstream::open(); ifstream::read(file_length);
)==fread()
?或者我应该使用
ifstream::rdbuf()->read()
?编辑:我的readFile()方法现在看起来像这样:
void readFile() { std::ifstream fin; fin.open("largefile.dat", ifstream::binary | ifstream::in); // in each of these small read methods, there are at least 1 fin.read() // call inside. readHeaderInfo(fin); readPreference(fin); readMainContent(fin); readVolumeData(fin); readTextureData(fin); fin.close(); }
使用小方法的多个fin.read()调用会减慢程序吗? 我只能在main方法中使用1个fin.read()并将缓冲区传递给小方法吗? 我想我打算写一个小程序进行测试。
谢谢!
Which is faster?
ifstream
orfread
.
Which should I use to read binary files?
fread()
puts the whole file into the memory.
So afterfread
, accessing the buffer it creates is fast.Does
ifstream::open()
puts the whole file into the memory?
or does it access the hard disk every time we runifstream::read()
?So... does
ifstream::open()
==fread()
?
or (ifstream::open(); ifstream::read(file_length);
) ==fread()
?Or shall I use
ifstream::rdbuf()->read()
?edit: My readFile() method now looks something like this:
void readFile() { std::ifstream fin; fin.open("largefile.dat", ifstream::binary | ifstream::in); // in each of these small read methods, there are at least 1 fin.read() // call inside. readHeaderInfo(fin); readPreference(fin); readMainContent(fin); readVolumeData(fin); readTextureData(fin); fin.close(); }
Will the multiple fin.read() calls in the small methods slow down the program? Shall I only use 1 fin.read() in the main method and pass the buffer into the small methods? I guess I am going to write a small program to test.
Thanks!
原文:https://stackoverflow.com/questions/6131307
最满意答案
样品
#include <stdio.h> #include <stdlib.h> #define N 4 #define M 3 int main() { int *Ptr; int (*p)[M]; int i,j; Ptr = malloc(M*N*sizeof(int)); for (i = 0; i < M * N; i++){ *(Ptr + i) = 1 + rand()%10; // printf("%d ", Ptr[i]); } // printf("\n"); p=(int (*)[M])Ptr;//p[N][M] for(i = 0; i < N ;++i){ for(j = 0; j < M;++j) printf("%d ", p[i][j]); printf("\n"); } return 0; }
Sample
#include <stdio.h> #include <stdlib.h> #define N 4 #define M 3 int main() { int *Ptr; int (*p)[M]; int i,j; Ptr = malloc(M*N*sizeof(int)); for (i = 0; i < M * N; i++){ *(Ptr + i) = 1 + rand()%10; // printf("%d ", Ptr[i]); } // printf("\n"); p=(int (*)[M])Ptr;//p[N][M] for(i = 0; i < N ;++i){ for(j = 0; j < M;++j) printf("%d ", p[i][j]); printf("\n"); } return 0; }
相关问答
更多-
用于学校教学的电脑网络教室,它的网络类型属于() A.互联网 B.广域网 C.城域网 D.局域网[2023-01-12]
d -
就像是.... for (int y = 0; y<8; y++) { int val = b[y]; for (int x=0; x<8; x++) { x==val ? printf("1") : printf("0") ; } printf("\n"); } 要么 for (int y = 0; y<8; y++) { char[9] line; strcpy(line,"00000000"); line[b[y] ...
-
将1D阵列合并为2D阵列(Merging 1D arrays into a 2D array)[2024-02-19]
考虑可扩展性。 如果我们增加数组的大小,那么完全numpy命令解决方案会更快: np.random.seed(1234) X = np.random.rand(10000) y = np.random.rand(10000) %timeit np.array(list(map(lambda i: [X[i],y[i]], range(len(X))))) 6.64 ms ± 32.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) %tim ... -
使用单个for循环。 在每次迭代中,将cols编号添加到i ,并从OneD阵列切片i和i + cols之间的项目。 将切片的项目推入twoD数组。 var OneD = ["Bristol","Cardiff","Birmingham","Luton","Swansea","Aberdeen","Birmingham","Manchester","Southampton","Chester","Swansea","Brighton","Portsmouth","Bournemouth","Glasgow", ...
-
我只是通过Array.prototype.intersect()的简单发明来做到这一点,其余的就是这么简单的Array.prototype.intersect() 。 Array.prototype.intersect = function(a) { return this.filter(e => a.includes(e)); }; var player1 = ["t1","t2","t3","t5","t7"], winmoves = [["t1","t2","t3"],["t4" ...
-
样品 #include
#include #define N 4 #define M 3 int main() { int *Ptr; int (*p)[M]; int i,j; Ptr = malloc(M*N*sizeof(int)); for (i = 0; i < M * N; i++){ *(Ptr + i) = 1 + rand()%10; // printf("%d ", Ptr ... -
你正在寻找一个“拉链”功能 使用Underscore.js,压缩数组变得简单 http://underscorejs.org/#zip var A = [1,3,5]; var B = [2,4,6]; var zipped = _.zip(A,B); // => [[1,2], [3,4], [5,6]] You're looking for a "zip" function With Underscore.js, zipping arrays made easy http://underscorejs ...
-
如何通过JNI获得普通的1D阵列而不是2D阵列?(How to get just an ordinary 1D array instead of a 2D array via JNI?)[2020-02-21]
所以,你的Java方法将是 static native void sumTraces(float[], int); 和它的C ++方面 JNIEXPORT void JNICALL sumTraces (JNIEnv* env, jclass caller, jfloatArray jPrestackArray, jint count); 现在,你可以使用 float* prestack1D = (float*)env->GetPrimitiveArrayCritical(jPrestackAr ... -
您需要在数组B插入额外的维度: A < B[:, None] 这允许NumPy正确匹配两种形状进行广播; B现在有形状(4,1),尺寸可以配对: (4, 100) (4, 1) 规则是维度具有相同的长度,或者其中一个长度需要为1; 这里100可以与1配对,4可以与4配对。在插入新尺寸之前,NumPy尝试将100与4配对,从而引发错误。 You need to insert an extra dimension into array B: A < B[:, None] This allows Num ...