I found this:
Performance: C# vs C++ « Systematic Gaming
This bloke reckons he gets 10 times the performance out of C++ than C# on a simple sort of increasing sized arrays, which seemed unlikely. So I tried his code, and for me it came out more like 3 times the performance from C++.
However, he'd used an unsafe fixed struct, which others on that site had said kills the performance, and sure enough when I replaced it with a regular class the C# version was about twice as fast as C++. Bugger.
But there's a fairly obvious difference in that the C++ version is making lots of extra copies, so I fixed it using rValue references (exactly what it's meant to solve). And phew, the C++ version is 2-3 times faster than C#.
I also changed his version to include the memory allocation and deallocation in the timing, which I thought was a fairer real world test. This means that C++ has one extra step in that it does the deallocation, whereas GC in C# means it sort of maybe does the deallocation next Tuesday. But I think that's fair as that's how things are done.
The other difference is that C++ can allocate an array of objects, but C# has to allocate an array and then new each member of the array, so that's a whole extra step and a whole load of extra newing. But that's what C# makes you do.
Other than that it comes down to std::sort vs. Array.Sort<T> which presumably are well optimised.
Interesting n'est-ce pas? Hardly a comprehensive test, but I can't see where the performance advantage is coming from unless it is the .NET runtime / JIT compiler / GC being less efficient. And that's pretty fundamental.
I'll post the code if anybody cares. Maybe somebody with more C# experience than me can do a better job.
Performance: C# vs C++ « Systematic Gaming
This bloke reckons he gets 10 times the performance out of C++ than C# on a simple sort of increasing sized arrays, which seemed unlikely. So I tried his code, and for me it came out more like 3 times the performance from C++.
However, he'd used an unsafe fixed struct, which others on that site had said kills the performance, and sure enough when I replaced it with a regular class the C# version was about twice as fast as C++. Bugger.
But there's a fairly obvious difference in that the C++ version is making lots of extra copies, so I fixed it using rValue references (exactly what it's meant to solve). And phew, the C++ version is 2-3 times faster than C#.
I also changed his version to include the memory allocation and deallocation in the timing, which I thought was a fairer real world test. This means that C++ has one extra step in that it does the deallocation, whereas GC in C# means it sort of maybe does the deallocation next Tuesday. But I think that's fair as that's how things are done.
The other difference is that C++ can allocate an array of objects, but C# has to allocate an array and then new each member of the array, so that's a whole extra step and a whole load of extra newing. But that's what C# makes you do.
Other than that it comes down to std::sort vs. Array.Sort<T> which presumably are well optimised.
Code:
C# results: 1024 0.935831229946823, 2048 1.35896461701138, 4096 3.01492927173705, 8192 7.40412538465084, 16384 26.1312610960332, 32768 67.3828796676673, 65536 150.520059113658, 131072 339.230656410242, 262144 807.599520330098, 524288 1974.64114852586, C++ 1024 0.668617 2048 1.92414 4096 2.63548 8192 6.49269 16384 12.9487 32768 26.5373 65536 56.3965 131072 115.083 262144 238.201 524288 483.761 (values are milliseconds)
I'll post the code if anybody cares. Maybe somebody with more C# experience than me can do a better job.
Comment