[Mono-dev] C# unsafe code performance

Zoltan Varga vargaz at gmail.com
Tue Jun 20 04:18:19 EDT 2006


                                          Hi,

  gcc is an optimizing compiler with a decade of work behind it, so it
will probably
always be faster than mono on code like this. If you want better perf,
you should
move the given code into a C library and invoke it using p/pinvoke from C#.

                  Zoltan

On 6/20/06, Rusmin Susanto <rusminsusanto at yahoo.com> wrote:
>
> Hello.
> I know that unsafe code (especially pointer arithmetic feature) can improve
> performance. If I have arrays
>
>  double[] a1 = new double[100];
>  double[] a2 = new double[100];
>  double[] a3 = new double[100];
>  double[] a4 = new double[100];
>  double[] a5 = new double[100];
>
> and do the following:
>
>  fixed(double *d1 = a1)
>  fixed(double *d2 = a2)
>  fixed(double *d3 = a3)
>  fixed(double *d4 = a4)
>  fixed(double *d5 = a5)
>  for(int i = 0; i < 100; i++)
>   *(d1 + i) = *(d2 + i) + *(d3 + i) + *(d4 + i) +*(d5 + i);
>
> we will get a significant performance gain compared to normal array access
> using []
>
> However, the performance is still nowhere near gcc. C# + unsafe is a lot
> slower (around 60-70% slower) than gcc. The gcc code looks like this:
>
>  for(int i = 0; i < 100; i++)
>   *(d1 + i) = *(d2 + i) + *(d3 + i) + *(d4 + i) +*(d5 + i);
>
> where d1, d2, etc. are arrays of double.
>
> I execute each code (the C# version and gcc version) 500,000 times and
> measure the execution time. gcc just beats C# by miles.
> My question is:
>
> Is there any way we can make the C# performance closer to gcc (eg. 5-10%
> slower)?
>
> Thanks for your response.
>
>
>
> Rusmin
>
>
>  ________________________________
> Yahoo! Groups gets better. Check out the new email design. Plus there's much
> more to come.
>
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
>



More information about the Mono-devel-list mailing list