Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all languages used within .net Equally performant?

Tags:

c#

clr

f#

I know the "Sales pitch" answer is yes to this question, but is it technically true.

The Common Language Runtime (CLR) is designed as an intermediate language based on Imperative Programming (IP), but this has obvious implications when dealing with Declarative Programming (DP).

So how efficient is a language based on a different paradigm than the Imperative Style when implemented in the CLR?

I also get the feeling that the step to DP would incur an extra level of abstraction that might not model at all performant, would this be a fair comment?

I have done some simple tests using F# and it all looks great, but am I missing something if the programs get more complex?

like image 451
WeNeedAnswers Avatar asked Nov 30 '22 18:11

WeNeedAnswers


2 Answers

There is no guarantee that languages produce the same IL for equivalent code, so I can safely say that there is no guarantee that all .NET languages are equally performant.

However, if they produce the same IL output, then there is no difference.

like image 199
Lasse V. Karlsen Avatar answered Dec 04 '22 13:12

Lasse V. Karlsen


First of all, the wide range of languages on the .NET platform definitely contains languages that generate code with different performance, so not all languages are equally performant. They all compile to the same intermediate language (IL), but the generated code may be different, some languages may rely on Reflection or dynamic language runtime (DLR) etc.

However, it is true that the BCL (and other libraries used by the languages) will have the same performance regardless of what language do you call them from - this means that if you use some library that does expensive calculations or rendering without doing complex calculations yourself, it doesn't really matter which language you use to call it.

I think the best way to think about the problem is not to think about languages, but about different features and styles of programming available in those languages. The following lists some of them:

  • Unsafe code: You can use unsafe code in C++/CLI and to some point also in C#. This is probably the most efficent way to write certain operations, but you loose some safety guarantees.

  • Statically typed, imperative: This is the usual style of programming in C# and VB.Net, but you can also use imperative style from F#. Notably, many tail-recursive functions are compiled to statically typed, imperative IL code, so this also applies to some F# functions

  • Statically typed, functional: This is used by most F# programs. The generated code is largely different than what imperative category uses, but it is still statically typed, so there is no significant performance loss. Comparing imperative and functional is somewhat difficult as the optimal implementation looks quite different in both of the versions.

  • Dynamically typed: Languages like IronPython and IronRuby use dynamic language runtime, which implements dynamic method calls etc. This is somewhat slower than statically typed code (but DLR is optimized in many ways). Note that code written using C# 4.0 dynamic also falls into this category.

There are many other languages that may not fall into any of these categoires, however I believe that the above list covers most of the common cases (and definitely covers all Microsoft languages).

like image 37
Tomas Petricek Avatar answered Dec 04 '22 13:12

Tomas Petricek