>> I do have a List<byte[]> with one million of byte[] entries (each of tem
>> has 3 values) to convert. It takes 1 - 2 seconds to convert. And it is
>> too slow for my case. What is the native code to convert it with faster
>> performance? I don't mind how ugly the code looks like as long as the
>> performance is best in my case.
How are you iterating through the list? Are you using any virtual calls
(through an IList or IEnumerable interface, for example)? Retrieving an
item from the List is probably several times slower than the BitConverter
call.
> On my PC it takes about 0.043 seconds to convert each byte[3] to long
> in a list with one million elements.
What? According to the code for ToInt64, that would throw.
if (startIndex > (value.Length - 8))
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
}
> Actually I had to test with ten million elements and divide by 10,
> because I could not really measure with so small data as one
[quoted text clipped - 9 lines]
> do it faster that BitConverter that has to handle all
> 8 bytes.
To start with, just use ToInt32 instead...
> The 0.043 seconds are with:
>
[quoted text clipped - 6 lines]
> return BitConverter.ToInt64(tmp, 0);
> }
Ahh, that's not a byte[3] after all.
> but using:
>
[quoted text clipped - 10 lines]
>
> Arne
Arne Vajhøj - 06 May 2008 02:03 GMT
>>> I do have a List<byte[]> with one million of byte[] entries (each of tem
>>> has 3 values) to convert. It takes 1 - 2 seconds to convert. And it is
[quoted text clipped - 6 lines]
> item from the List is probably several times slower than the BitConverter
> call.
I would expect retrieving a ref to the byte[] from a List<byte[]> to
be much faster than BitCoverter.ToInt64, but I have not measured it
and I can be completely wrong.
>> On my PC it takes about 0.043 seconds to convert each byte[3] to long
>> in a list with one million elements.
[quoted text clipped - 5 lines]
> ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
> }
>> The 0.043 seconds are with:
>>
[quoted text clipped - 8 lines]
>
> Ahh, that's not a byte[3] after all.
Let us just say that I got an exception in first attempt ...
:-)
Arne
Ben Voigt [C++ MVP] - 08 May 2008 15:05 GMT
>>>> I do have a List<byte[]> with one million of byte[] entries (each
>>>> of tem has 3 values) to convert. It takes 1 - 2 seconds to
[quoted text clipped - 11 lines]
> be much faster than BitCoverter.ToInt64, but I have not measured it
> and I can be completely wrong.
BitConverter.ToInt64 ought to be much faster than a virtual call, because it
will be inlined and there is no need for a pipeline flush.
In fact, unless you're dealing with a non-native byte order,
BitConverter.ToInt64 should reduce to just a 64-bit load.
I wouldn't be surprised to learn that the JIT generates considerably less
efficient code, though.
Arne Vajhøj - 12 May 2008 01:40 GMT
>>> How are you iterating through the list? Are you using any virtual
>>> calls (through an IList or IEnumerable interface, for example)?
[quoted text clipped - 12 lines]
> I wouldn't be surprised to learn that the JIT generates considerably less
> efficient code, though.
It is not just the shift's and or's. It is also all the if's.
Arne