all groups > dotnet performance > august 2003 >
You're in the

dotnet performance

group:

Performance of operator overloading with structs is extremely low.


Performance of operator overloading with structs is extremely low. Rüdiger
8/28/2003 1:38:46 AM
dotnet performance:
Operator overloading is a nice feature for things like vector and matrix
libraries, but I have found that the performance is so low that I can not
use it.

I have appended a small sample program to illustrate the effect. The
performance of the loop using operator overloading is 1/4 of the loop using
the ugly c-style static method.

It seems that as of now there are two choices in .NET: use all the nice
abstractions such as properties, operator overloading and indexers and
accept the resulting horrible performance, or write ugly c-style code using
lots of ref parameters and direct member access and get acceptable
performance.

When I use C++ I can use nice abstractions like operator overloading without
suffering a large performance penalty. I see no technical reason why this
should not be possible in .NET.

This is a big problem for me. I wrote a vector and matrix library that makes
heavy use of operator overloading. Now I have to decide wether to rewrite
everything using ugly C style to get decent performance, or just wait until
the performance of the .NET JIT-Compiler becomes acceptable.

The question is: when will the performance improve?

regards,

Re: Performance of operator overloading with structs is extremely low. Andre
8/28/2003 10:52:08 AM
I disagree. Your test is not fair. With static methods, you're providing
three vectors, the ones that need to be added and the one in which the
result is stored. With operator overloading, you're returning a Vector
object. Do the same with the static methods and you'll see there's no
difference.

The point is, it's not because of operator overloading that you're
seeing this difference, it's because of the way you designed your
program. The design of a program has a large affect on overall
performance. Now consider an equation like this: c = sqrt((a/b*c)-d%e)

You'll spend the rest of your life debugging equations like these.

-Andre


[quoted text, click to view]
Re: Performance of operator overloading with structs is extremelylow. Andre
8/28/2003 11:13:44 AM
[quoted text, click to view]

By this I meant if you have similar looking lines in your code:

[quoted text, click to view]

also note that you would essentially want to return an object of type
Vector in your static sum() method if you're thinking about serious
reuse of your library. In effect, you would see similar results as to
what you saw with operator overloading.

-Andre

[quoted text, click to view]
Re: Performance of operator overloading with structs is extremelylow. Andre
8/28/2003 11:25:13 AM
Sorry for multiple posts :) but..

[quoted text, click to view]

remove the word "static" from "static sum()".. my mistake

[quoted text, click to view]
Re: Performance of operator overloading with structs is extremely low. Rüdiger
8/28/2003 11:39:31 AM
[quoted text, click to view]
Of course the two methods work different. One is ugly and fast, the other
one looks good but is slow as hell.

The way operator overloading works in .NET, I have no choice but to create a
temporary object for the return value. This should be no big deal since it
is a struct and thus created on the stack. The JIT-compiler should see that
it is just a temporary struct and optimize it away. But it does not. That
is the problem.

If you do not believe me, try the above sample in C++. You will see that
both loops run at exactly the same speed. In fact a good C++ compiler might
optimize away the loop completely since the vector c is not used after the
loop.

regards,

Re: Performance of operator overloading with structs is extremely low. David Notario
8/28/2003 9:07:59 PM
Rüdiger is correct, these are limitations in the current JIT compiler . Of
course, we're not happy about them. They should be addressed in the future.
--
David Notario
Software Design Engineer - CLR JIT Compiler
http://xplsv.com/blogs/devdiary/

[quoted text, click to view]

AddThis Social Bookmark Button