Page 1 of 1

FileStream/BinaryWriter Speed Issue

Posted: Thu Feb 26, 2009 10:04 am
by JacksonCougAr

Code: Select all

private void InsertBytesInMap(byte[] bytesToInsert, int insertAt)
        {
            Benchmark.Begin();
            string MapFolder = sourceMap.FilePath.Remove(sourceMap.FilePath.LastIndexOf('\\')+1);
            FileStream temp = new FileStream(MapFolder + "BIN-01.bin", FileMode.Create, FileAccess.ReadWrite);
            sourceMap.Mark();
            sourceMap.Position = 0;
            Status("Writing MapStream To BIN File...");
            temp.Write(sourceMap.ReadBytes(insertAt), 0, insertAt);
            Status("Saving...");
            temp.Flush();
            Status("Closing BIN File...");
            temp.Close();
            Status("Writing MapStream To BIN File...");
            temp = new FileStream(MapFolder + "BIN-02.bin", FileMode.Create, FileAccess.ReadWrite);
            temp.Write(sourceMap.ReadBytes((int)(sourceMap.Length - insertAt)), 0, (int)(sourceMap.Length - insertAt));
            Status("Saving...");
            temp.Flush();
            Status("Closing BIN File...");
            temp.Close();
            Status("Closing MapStream...");
            sourceMap.Close();
            Status("Creating New Map File...");
            FileStream newMap = new FileStream(sourceMap.FilePath, FileMode.Create, FileAccess.ReadWrite);
            BinaryWriter newMapWriter = new BinaryWriter(newMap);
            Status("Opening BIN File...");
            temp = new FileStream(MapFolder + "BIN-01.bin", FileMode.Open, FileAccess.Read);
            BinaryReader tempReader = new BinaryReader(temp);
            Status("Writing Data To New Map File From BIN File...");
            newMapWriter.Write(tempReader.ReadBytes((int)temp.Length));
            Status("Saving...");
            newMapWriter.Flush();
            temp.Flush();
            Status("Closing BIN File...");
            temp.Close();
            Status("Deleting BIN File...");
            File.Delete(temp.Name);
            Status("Writing New Data Bytes To New Map File...");
            newMapWriter.Write(bytesToInsert);
            Status("Saving...");
            newMapWriter.Flush();
            Status("Opening BIN File...");
            temp = new FileStream(MapFolder + "BIN-02.bin", FileMode.Open, FileAccess.Read);
            tempReader = new BinaryReader(temp);
            Status("Writing Data To New Map File From BIN File...");
            newMapWriter.Write(tempReader.ReadBytes((int)temp.Length));
            Status("Saving...");
            newMapWriter.Flush();
            Status("Closing New Map File...");
            newMapWriter.Close();
            newMapWriter = null;
            Status("Saving...");
            temp.Flush();
            Status("Closing BIN File...");
            temp.Close();
            Status("Deleting BIN File...");
            File.Delete(temp.Name);
            temp = null;
            sourceMap.Close();
            Benchmark.End();
        }
Running this through Headlong will take anywhere between 6-13 seconds. I was wondering if anybody knew how this might be sped up?

Re: FileStream/BinaryWriter Speed Issue

Posted: Thu Feb 26, 2009 3:25 pm
by xzodia
...use a byte array -_-

Re: FileStream/BinaryWriter Speed Issue

Posted: Thu Feb 26, 2009 7:26 pm
by LuxuriousMeat
xzodia wrote:...use a byte array -_-
Yea, just place them in a temporary byte array, the way your doing it is writing so much unnecessary data! By using a byte array it should cut the time in half. (But don't quote me on that :-D)

Re: FileStream/BinaryWriter Speed Issue

Posted: Thu Feb 26, 2009 9:11 pm
by JacksonCougAr
Resolved Through Xbox7887, Thanks to all those who responded anyways. I realize now just how much of a waste that method was :p