Or you could just use a BinaryWriter and a memory stream .. the binary
writer actually does this in a more effiicient way than you do..
reflector'ed source of write method.
public virtual void Write(long value)
{
this._buffer[0] = (byte) value;
this._buffer[1] = (byte) (value >8);
this._buffer[2] = (byte) (value >0x10);
this._buffer[3] = (byte) (value >0x18);
this._buffer[4] = (byte) (value >0x20);
this._buffer[5] = (byte) (value >40);
this._buffer[6] = (byte) (value >0x30);
this._buffer[7] = (byte) (value >0x38);
this.OutStream.Write(this._buffer, 0, 8);
}
Cheers,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
<AK_TIREDOFSPAM@hotmail.COMwrote in message
news:1155835940.117869.197730@75g2000cwc.googlegro ups.com...
Quote:
Thank you Greg. I am comparing different ways to send a large array of
unsigned longs to SQL Server 2000. The solution needs to be very
performant. Among other alternatives, I am sending to the database an
image, as follows:
>
SqlCommand a = new SqlCommand("ImageTest", conn);
a.CommandType = System.Data.CommandType.StoredProcedure;
a.Parameters.Add(new SqlParameter("@image",
System.Data.SqlDbType.Image,2147483647));
-- b is an array of bytes
a.Parameters[0].Value = b;
>
My understanding is that BitConverter will create a new array of bytes
for every unsigned long, and I think that would be hard on the garbage
collector. My understanding is that most likely I will not notice that
in a short test on my local PC, but eventually in the production that
would cause a slowdown. So I estimate that in my situation it is better
to swap then bytes manually so that there are only 2 new objects as
opposed to 10001:
>
MemoryStream ms = new MemoryStream(80000);
BinaryWriter br = new BinaryWriter(ms);
for(ulong i=1; i<10001; i++)
{
br.Write(i);
}
byte[] b = ms.ToArray();
byte[] b1 = new byte[80000];
--- swap the bytes manually
for(k=0; k<80000; k+=8)
{
for(j=0; j<8; j++)
{
b1[k+7-j] = b[k+j];
}
}
>
Please correct me if I am wrong.
>