Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance difference between C++ and C# for mathematics

I would like to preface this with I'm not trying to start a fight. I was wondering if anyone had any good resources that compared C++ and C# for mathematically intensive code? My gut impression is that C# should be significantly slower, but I really have no evidence for this feeling. I was wondering if anyone here has ever run across a study or tested this themselves? I plan on running some tests myself, but would like to know if anyone has done this in a rigorous manner (google shows very little). Thanks.

EDIT: For intensive, I mean a lot of sin/cos/exp happening in tight loops

like image 765
Steve Avatar asked Sep 30 '09 20:09

Steve


People also ask

Is C or C faster?

Performance-based on Nature Of Language C++ language is an object-oriented programming language, and it supports some important features like Polymorphism, Abstract Data Types, Encapsulation, etc. Since it supports object-orientation, speed is faster compared to the C language.

How much faster is C vs C++?

C is faster than C++ C++ allows you to write abstractions that compile-down to equivalent C. This means that with some care, a C++ program will be at least as fast as a C one. The advantage C++ gives over C is that it enables us to also build reusable abstractions with templates, OOP and functional composition.

Is C faster than Objective C?

Objective-C is slightly slower than straight C function calls because of the lookups involved in its dynamic nature.


1 Answers

I have to periodically compare the performance of core math under runtimes and languages as part of my job.

In my most recent test, the performance of C# vs my optimized C++ control-case under the key benchmark — transform of a long array of 4d vectors by a 4d matrix with a final normalize step — C++ was about 30x faster than C#. I can get a peak throughput of one vector every 1.8ns in my C++ code, whereas C# got the job done in about 65ns per vector.

This is of course a specialized case and the C++ isn't naive: it uses software pipelining, SIMD, cache prefetch, the whole nine yards of microoptimization.

like image 182
Crashworks Avatar answered Sep 29 '22 15:09

Crashworks