You should not use fseek in the io inner loop of operations. To make recording functions fast, they cache records. If you search everywhere, you continue to blow into the cache.
Perform all of your in-memory conversions β for example, put a cube in memory, and then write the file to several sequential fwrite calls.
If you cannot completely convert your data into memory, then collect a cube one plane at a time in memory and write out each plane.
@edit:
In your case, you do not want to use fseek at all. Not even one.
Do something like this:
void writeCubeZYX( int* cubeXYZ, int sizeOfCubeXYZ, FILE* file ) { int* cubeZYX = malloc( sizeOfCubeXYZ );
@ edit2:
Well, suppose you cannot convert all of this into memory, and then convert it to a plane and write one plane at a time - in file order - without fseeks.
So, the cube [XYZ] is laid out in memory as a series of matrices Z [XY]. That is, the [XY] planes of your cube are adjacent in memory. And you want to record as [ZYX]. Thus, in the file you want to write a series of X [ZY] matrices. Each [ZY] will be contiguous in the file.
So you are doing something like this:
void writeCubeZYX( int* cubeXYZ, int x, int y, int z, FILE* file ) { int sizeOfPlaneZY = sizeof( int ) * y * z; int* planeZY = malloc( sizeOfPlaneZY ); for ( int i = 0; i < X; i++ ) {
source share