Re: are arrays contiguous in memory?
Willy Denoyette [MVP] wrote:[color=blue]
> Agreed, all I want is to make sure people don't start to believe that
> using pointer arithmetic has performance advantages when accessing
> arrays (elements). I know a lot of C# (and some C++) developers believe
> that there is such advantage when using unsafe constructs, but it's
> most often not the case, worse, they start to pin arrays for a long
> period of time, effectively disturbing the GC activity which results in
> reduced overall performance.[/color]
There is one performance benefit for using pointers over indexing for CLR
arrays. When indexing a CLR array, there is a bounds check. That check is
eliminated when using a pointer to iterate over the array.
Of course, that's a trade off between safety and performance. One should be
very careful when making that decision. Because the JIT compiler has the
ability to eliminate bounds checks in some cases, switching to pointers is
not always a worthwhile exercise. Though, it is definitely useful in some
cases where bitmap manipulation is done. This is probably the only reason
why interior pointers exist in the new C++. If it wasn't for this case, I
probably would have succeeded at keeping them out of the language entirely.
--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/
Willy Denoyette [MVP] wrote:[color=blue]
> Agreed, all I want is to make sure people don't start to believe that
> using pointer arithmetic has performance advantages when accessing
> arrays (elements). I know a lot of C# (and some C++) developers believe
> that there is such advantage when using unsafe constructs, but it's
> most often not the case, worse, they start to pin arrays for a long
> period of time, effectively disturbing the GC activity which results in
> reduced overall performance.[/color]
There is one performance benefit for using pointers over indexing for CLR
arrays. When indexing a CLR array, there is a bounds check. That check is
eliminated when using a pointer to iterate over the array.
Of course, that's a trade off between safety and performance. One should be
very careful when making that decision. Because the JIT compiler has the
ability to eliminate bounds checks in some cases, switching to pointers is
not always a worthwhile exercise. Though, it is definitely useful in some
cases where bitmap manipulation is done. This is probably the only reason
why interior pointers exist in the new C++. If it wasn't for this case, I
probably would have succeeded at keeping them out of the language entirely.
--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/
Comment