views:

38

answers:

1

Here is my current swap code for swapping 2 KeyValuePair objects in an array:

KeyValuePair<int, T> t = a[i];
            a[i] = a[j];
            a[j] = t;

Would there be any speed advantage to using unsafe code and merely swapping the pointers of the 2 objects? Or does the complier effectively boil this safe code down to effectively doing just that?

+4  A: 

No, it won't be any faster.

This is premature micro-optimization at its worst.

In fact, it will be orders of magnitude slower, since you'll need to pin the array (using the fixed keyword) in order to get a pointer to it.

SLaks
Not to mention that with marshalling data across managed/unmanaged boundaries, it will most likely be WORSE.
Blindy
@Blindy: Wrong; that's only for COM and P/Invoke.
SLaks
Just asking purely from an "understanding the framework" point of view.
IanC
@IanC: Using pointers will be **much** slower.
SLaks