C# Programming
03-13-2010, 03:51 AM
Hi,
I have two methods, which are esentially doing similar jobs but have drastic speed differences. I don't know what is causing this speed difference. Here are the methods. The first one is like 100 times faster.
private void SaveNativeBinary(string file, long origin, long length, string opt)
{
SetSSVisible(StatusStrip, pbSmallPerc, true);
FileStream fs = null;
FileStream writer = null;
try { fs = new FileStream(file, FileMode.Open, FileAccess.Read); }
catch { throw new Exception("ERR"); }
try { writer = new FileStream(opt, FileMode.Create, FileAccess.ReadWrite); }
catch { throw new Exception("ERR"); }
fs.Seek(origin, SeekOrigin.Begin);
for (long pos = 0; pos < length; pos++)
{
if (pos % 204800 == 0) // 200KB'da bir güncelle
{
SetSSValue(StatusStrip, pbSmallPerc, Convert.ToInt32(PercentageCalc(pos, length)) );
SetSSText(StatusStrip, lblStatus, "Aktar?l?yor: " + BtoKB(pos).ToString() + " / " + BtoKB(length).ToString() + " KB (%" + PercentageCalc(pos, length) + ")");
}
writer.WriteByte((byte)fs.ReadByte());
}
fs.Close(); fs.Dispose(); writer.Close(); writer.Dispose();
SetSSVisible(StatusStrip, pbSmallPerc, false);
}
private void AppendBinToBin(string Container, string toAppend)
{
SetSSVisible(StatusStrip, pbSmallPerc, true);
FileStream fsA, fsB = null;
long tempLength = GetFileLength(toAppend);
try { fsA = new FileStream(Container, FileMode.Open, FileAccess.ReadWrite); }
catch { throw new Exception("ERR"); }
try { fsB = new FileStream(toAppend, FileMode.Open, FileAccess.Read); }
catch { throw new Exception("RRR"); }
try {
fsA.Seek(0, SeekOrigin.End);
for (long pos = 0; pos < fsB.Length; pos++)
{
if (pos % 204800 == 0) // 200KB'da bir güncelle
{
SetSSValue( StatusStrip, pbSmallPerc, Convert.ToInt32(PercentageCalc(pos, tempLength)) );
SetSSText( StatusStrip, lblStatus, "Aktar?l?yor: " + BtoKB(pos).ToString() + " / " + BtoKB(tempLength).ToString() + " KB (%" + PercentageCalc(pos, tempLength) + ")" );
}
fsA.WriteByte( (byte)fsB.ReadByte() );
}
fsB.Close(); fsB.Dispose(); fsA.Close(); fsA.Dispose();
}
catch { throw new Exception("ERR."); }
SetSSVisible(StatusStrip, pbSmallPerc, false);
}
I am aware of the useless try-catch blocks, don't worry about them. They are not the cause of this speed difference.
Any help is appriciated.
Thanks.
I have two methods, which are esentially doing similar jobs but have drastic speed differences. I don't know what is causing this speed difference. Here are the methods. The first one is like 100 times faster.
private void SaveNativeBinary(string file, long origin, long length, string opt)
{
SetSSVisible(StatusStrip, pbSmallPerc, true);
FileStream fs = null;
FileStream writer = null;
try { fs = new FileStream(file, FileMode.Open, FileAccess.Read); }
catch { throw new Exception("ERR"); }
try { writer = new FileStream(opt, FileMode.Create, FileAccess.ReadWrite); }
catch { throw new Exception("ERR"); }
fs.Seek(origin, SeekOrigin.Begin);
for (long pos = 0; pos < length; pos++)
{
if (pos % 204800 == 0) // 200KB'da bir güncelle
{
SetSSValue(StatusStrip, pbSmallPerc, Convert.ToInt32(PercentageCalc(pos, length)) );
SetSSText(StatusStrip, lblStatus, "Aktar?l?yor: " + BtoKB(pos).ToString() + " / " + BtoKB(length).ToString() + " KB (%" + PercentageCalc(pos, length) + ")");
}
writer.WriteByte((byte)fs.ReadByte());
}
fs.Close(); fs.Dispose(); writer.Close(); writer.Dispose();
SetSSVisible(StatusStrip, pbSmallPerc, false);
}
private void AppendBinToBin(string Container, string toAppend)
{
SetSSVisible(StatusStrip, pbSmallPerc, true);
FileStream fsA, fsB = null;
long tempLength = GetFileLength(toAppend);
try { fsA = new FileStream(Container, FileMode.Open, FileAccess.ReadWrite); }
catch { throw new Exception("ERR"); }
try { fsB = new FileStream(toAppend, FileMode.Open, FileAccess.Read); }
catch { throw new Exception("RRR"); }
try {
fsA.Seek(0, SeekOrigin.End);
for (long pos = 0; pos < fsB.Length; pos++)
{
if (pos % 204800 == 0) // 200KB'da bir güncelle
{
SetSSValue( StatusStrip, pbSmallPerc, Convert.ToInt32(PercentageCalc(pos, tempLength)) );
SetSSText( StatusStrip, lblStatus, "Aktar?l?yor: " + BtoKB(pos).ToString() + " / " + BtoKB(tempLength).ToString() + " KB (%" + PercentageCalc(pos, tempLength) + ")" );
}
fsA.WriteByte( (byte)fsB.ReadByte() );
}
fsB.Close(); fsB.Dispose(); fsA.Close(); fsA.Dispose();
}
catch { throw new Exception("ERR."); }
SetSSVisible(StatusStrip, pbSmallPerc, false);
}
I am aware of the useless try-catch blocks, don't worry about them. They are not the cause of this speed difference.
Any help is appriciated.
Thanks.