[Mono-dev] C# unsafe code performance

tcmichals tcmichals at msn.com
Tue Jun 20 10:16:57 EDT 2006


One of the biggest issues is the pointer calculation, if you change the d1+i 
to d1++ you will see a large performance increase, you just reassign the 
fixed pointer to another pointer

for example fixed ....
{
    double *p1 = d1;  now you can do *p1++
}

FYI: the C++ compiler does an optimize similar but it still beats the CLR 
generated code, better register optimization...
Since the memory is fixed, it shouldn't move, you get a bigger it is about a 
2x performance increase

"Zoltan Varga" <vargaz at gmail.com> wrote in message 
news:295e750a0606200118k3acfeb85s96c836149fea2903 at mail.gmail.com...
>                                          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