Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

AliasNotFound

Improving performance of .NET

This topic is 5209 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote a simple raytracer in C++ and then rewrote it into C#, but the the C# version runs about 3 times slower and I can't figure out why. Both projects were built in release mode, the structure of the code between the two projects is as close as I could get it. I used std::vector in the C++ version for my lists and ArrayList in the C# version. My Vector class (math vectors that is) used inlined operator overloading in the C++ version and in the C# version I implemented that with a struct and static operator overloads. When I ran the C# version through the profiler, it seems to spend about 1/3 of its time in mscorlib. In my code the most time seems to be taken up during the return from the overloaded operators in the Vector struct. Anyone know some things to try to speed this up? I wouldn't think it should be 3 times slower so I can only guess that there is something I'm doing wrong. Thanks, Phil [Edited to fix typo.] [edited by - AliasNotFound on April 10, 2004 6:06:19 PM]

Share this post


Link to post
Share on other sites
Advertisement
I''d check for excessive boxing/unboxing in your code. That seems to be a frequent cause of bad performance for C++ code moved to C#. For example, what kind of objects are you putting in your ArrayList?

Share this post


Link to post
Share on other sites
Raytracers are very floating point intensive applications.

.NET is surprizingly fast, but falls flat on its face when faces with floating point math. Microsoft has identified floating point optimizations as a feature that was cut from the 1.0 release of .NET due to time constraints and has been promised for version 2.0 which is due soon (they say and I hope).

You said you used a struct for your vector class, which is a good performance trick unless you are storing your vectors in those ArrayList objects you mentioned. If you are then (un)boxing is killing your performance. Look it up :-) the trick is to use typed C# arrays (which you will have to resize manually) that do not suffer from boxing costs. Generics (also coming in .NET 2.0) should solve this problem as well.

You said a lot of time was spent in mscorlib, can you tell us what functions specifically? Or post a text version of your profiler results? I could give you a better analysis and optimization tips.

-SniperBoB-

Share this post


Link to post
Share on other sites
quote:
Original post by GnuVince
Have you tried profiling your code?


quote:

When I ran the C# version through the profiler, it seems to spend about 1/3 of its time in mscorlib. In my code the most time seems to be taken up during the return from the overloaded operators in the Vector struct. Anyone know some things to try to speed this up? I wouldn''t think it should be 3 times slower so I can only guess that there is something I''m doing wrong.



Read the post before replying

Share this post


Link to post
Share on other sites
I really recommend this book as it tells you important facts about the framework that can help you to improve performance and overall effectivity of your programs.

Regards,
Andre (VizOne) Loker

Share this post


Link to post
Share on other sites
quote:
Original post by SnprBoB86

You said a lot of time was spent in mscorlib, can you tell us what functions specifically? Or post a text version of your profiler results? I could give you a better analysis and optimization tips.

-SniperBoB-


It seems to spend the most time in WaitForSingleObject and ArrayListEnumeratorSimple.MoveNext.

The only items I am putting into the array lists are the light and world objects, (spheres, planes, etc.). But of course they all get iterated over during each call to my trace and shade methods. I don''t belvied there is any boxing/unboxing going on. My Vector obects are never put into an array list. There does seem to be an inordinate amount of time spent returning from the overloaded operator functions though.

Share this post


Link to post
Share on other sites
If you put anything that''s a value object (integers, structs, etc) into an ArrayList, then boxing/unboxing happens. Perhaps you already knew this, but it''s worth mentioning. If you''re spending a lot of your time in MoveNext, then the iterator cost of ArrayList is eating you; you should try using typed arrays instead (you can have typed arrays of class instances, too, which translates to arrays of instance pointers in C++).

Share this post


Link to post
Share on other sites
Well, I replaced the ArrayLists with typed arrays and got about a 30% improvement. Still twice as slow as the C++ version. I wonder how much of the deficientcy is in that floating point problem that was mentioned earlier?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by AliasNotFound
It seems to spend the most time in WaitForSingleObject...
Have you investigated this further? Are you having multiple threads in your application? Is your app CPU bound? Are you by any chance running the C# version under debugger (that would prevent all JIT optimizations)?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!