Hello, all.
I became aware of issues with fwrite() a couple years ago, as it threw monkey wrenches in my data management, and I quickly wrote a function to work around this and replace it. But recently, I have decided that my current method is a serious bottleneck, and I was wondering if using fwrite() to write an array of single bytes would be fatal to portability. My current integer writing function, seen below, I've revised and optimized several times and squeezed as much as I can out of it, performance-wise. The datatypes are self explanitory and defined elsewhere; (signedness)(ty pe)(bitcount). So ui32 is an unsigned 32 bit integer, etc.
Currently, I am having the issue where n*size putc() calls is a lot slower than one fwrite() per size bytes, until a buffer fills and writes out. What I am concerned with, is breaking portability. I noticed years back that so much as recompiling the program somehow made my files unusable, even though I never write structures as a whole or anything of the sort. By the way, endVar is a variable containing the endianness state; 0 for LSB and 1 for MSB. Determined through a quick routine in the constructor for my IO class, part of my library. I digress. Main question, is using fwrite() to write blocks of single bytes unhealthy, if the same file were to be used on many platforms and such? Or is my current function the best I am going to get for my purposes?
I became aware of issues with fwrite() a couple years ago, as it threw monkey wrenches in my data management, and I quickly wrote a function to work around this and replace it. But recently, I have decided that my current method is a serious bottleneck, and I was wondering if using fwrite() to write an array of single bytes would be fatal to portability. My current integer writing function, seen below, I've revised and optimized several times and squeezed as much as I can out of it, performance-wise. The datatypes are self explanitory and defined elsewhere; (signedness)(ty pe)(bitcount). So ui32 is an unsigned 32 bit integer, etc.
Code:
ui32 Ectaraio::write( const void * ptr, ui32 size, ui32 n){ ui32 numWritten=0; si8 * p = (si8*)ptr; if(size == 1){ for(ui32 i = n; i--;){ putc(*(p++),fp); numWritten++; } }else{ if(!endVar){ for(ui32 varIndex=n;varIndex--;){ p+=size; for(ui32 byteIndex=size;byteIndex--;){ putc(*(--p),fp); numWritten++; if(!verbose)continue; sf32 percent = (sf32)numWritten/(size*n)*100; if(((si32)percent%5==0))printf("%.2f%%\n",percent); } p+=size; } }else{ for(ui32 varIndex=n;varIndex--;){ for(ui32 byteIndex=size;byteIndex--;){ putc(*(p++),fp); numWritten++; if(!verbose)continue; sf32 percent = (sf32)numWritten/(size*n)*100; if(((si32)percent%5==0))printf("%.2f%%\n",percent); } } } } return numWritten; }
- Ectara
Comment