Pointers?

  • Thread starter Thread starter O.o
  • Start date Start date
O

O.o

Hi all,

Is there any advantage in C# to using a pointer to access members of
an array? On some processors (esp. DSP) it can be significantly faster
than using the array index in an incremental loop, but some compilers
are smart enough to recognize that and optimize accordingly. Does
anybody know offhand how good a job the VS 08 compiler does at this?

Regards,

-S
 
Hi all,

Is there any advantage in C# to using a pointer to access members of
an array? On some processors (esp. DSP) it can be significantly faster
than using the array index in an incremental loop, but some compilers
are smart enough to recognize that and optimize accordingly. Does
anybody know offhand how good a job the VS 08 compiler does at this?

The Visual Studio C# compiler has nothing to do with it. It emits MSIL,
an abstract language that is portable across .NET implementations. At
run-time, .NET's Just-In-Time compiler compiles that to the native
instructions needed for the given platform, performing necessary
optimizations for that platform.

And yes, you should assume that, unless you've seen some measurable
performance problem, the JIT compiler is capable of performing the
necessary optimizations. Use of pointers in a C# program should primarily
be for situations where managed data access simply won't work, rather than
an attempt to second-guess .NET's own optimizations.

Pete
 
To add to Peter's post, a good example of where I use pointers is in an
unsafe block for image processing (which the alternative qualifies as a
"measureable performance problem" if ever there was one).

--
Roger

"Logic Is Syntax Independent"
 
Thanks Pete and Roger for your input. I'm implementing a signal
filter, and it doesn't seem to be a bottleneck on the CPU I'm working
on. I'd prefer not to have to use pointers, because they're a PITA in
C# the way I want to use them.

-S
 
As long as you write your loops in such a way (e.g. foreach, or for i = 0; i
< a.Length; i++) that the compiler can figure out there's no
overflow/underflow, indexing should be just as fast as pointers.

It's the index-out-of-range checks that can kill performance if your
conditions are complicated enough to confuse the optimizer.
 
Back
Top