Sign in to follow this  
Sambori

[.net] C# Fast like hell

Recommended Posts

After an simple benchmark I did comparing how fast to build and iterate through a list of integers made of 1000,1000 elements, i was surprised to see that C# was fast like hell compared to C++, which was disappointingly slow. How come C# is fast that even faster than what should be the normal sense...

Share this post


Link to post
Share on other sites
Your benchmark is invalid. It probably doesn't tell you what you think it tells you, if that.

Why don't you provide us the code, input, and output for the benchmark and described the environment you built (for example, did you enable optimizations?) and executed it under.

Share this post


Link to post
Share on other sites
When i was working with C#, as a learning exercise I made a simple gravity particle simulator, this was on XNA. Storing all the particles in large arrays I was about to push about 300k particles with 3 gravity wells at 20fps. I ported it over to C++, surprisingly the performance was nearly the same! It was only after reimplementing the algorithm in SIMD was i able to beat the C# version, by a factor of ~3. So I would say C# will give acceptable performance for most things.

Your experience may vary :D

Enjoy!

-ddn

Share this post


Link to post
Share on other sites
After going throug the code again and trying to replace the C++ list with array, then C++ is 10 times faster. And in fact C# List implementation is actually faked by array so that it gives performance gain...nvm

Share this post


Link to post
Share on other sites
Quote:
Original post by Sambori
C# List implementation is actually faked by array
It's not faked at all, it's explicitly called out in the documentation that List<T> is implemented as an array.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sambori
After going throug the code again and trying to replace the C++ list with array, then C++ is 10 times faster. And in fact C# List implementation is actually faked by array so that it gives performance gain...nvm

Your benchmark is still invalid. It probably doesn't tell you what you think it tells you, if that.

Why don't you provide us the code, input, and output for the benchmark and described the environment you built (for example, did you enable optimizations?) and executed it under.

Share this post


Link to post
Share on other sites
Any c++ code being 10x faster than c# is ridiculous, clearly you're doing something wrong. Are you in debug mode? Are you using reference types for something that's frequently instanced?

In my experience, even extremely tight, mathematically complex code can have similar performance in c# or c++. I ported a c++ procedural noise library from c++ to c# and was able to get it to within 10% or so of the c++ performance (although admittedly it did require a lot of knowledge of writing optimized .net code to pull that off).

Again, while in my experience you can never get quite the same performance in c#, it's close enough to not be a big deal (and to be worth the more rapid development imo).

Share this post


Link to post
Share on other sites
Quote:
Original post by Sambori
After going throug the code again and trying to replace the C++ list with array, then C++ is 10 times faster. And in fact C# List implementation is actually faked by array so that it gives performance gain...nvm


List != linked list : both linked lists and dynamic arrays are implementations of the list abstract data type.


A C# List is a list, implemented as a dynamic array.
A C++ std::vector is a list, implemented as a dynamic array.

A C# LinkedList is a list, implemented as a linked list.
A C++ std::list is a list, implemented as a linked list.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this