File IO efficiency - how to improve noob code

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,

I am reading and writing arrays of data to my disk, and this is the code I use to do it.

<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Block
//...bunch of code leading to here
<span id="_ctl0_MainContent_PostFlatView <span>FileStream^ fs = gcnew FileStream("some file name",FileMode:pen);
<span id="_ctl0_MainContent_PostFlatView <span>BinaryReader^ br = gcnew BinaryReader(fs);
array<double,2>^ DIMAGE = gcnew array<double,2>(NAXIS1,NAXIS2);
if (BITPIX == 16)//bits per pixel,  theres several other ifs like this (for 8, 32, etc), but Ill just show you this one
    {
        __int16 val;
        for (int j = 0; j < NAXIS2; j++)
        {
            for (int i = 0; i < NAXIS1; i++)
            {
                val = ShortSwap(br->ReadInt16());//need to swap bytes because files are big endian
                DIMAGE[i,j] = double(val)*(double)BSCALE + (double)BZERO;//want the DIMAGE in double precision
            }
        }
    }

//heres my ShortSwap function just in case you wanted to see it
short ShortSwap(short s)
{
    return ((s>>8)&0xff) + ((s << 8)&0xff00);
}


So the question is:  In the code above where I read the values off the disk into the array, can this be made faster by first reading all the bytes into a buffer, and then dumping that data into the array in the correct format...or something?  Ive seen other people do it this way (I think) but dont understand what the process would be.  I do know that it can be made faster though.  I write the data much like the code above too.
Using VS C++ 2005 btw.


Thanks

Joe


View the full article
 
Back
Top