Is the Managed Code Slower than the Native Code?

That is a question that I have been answering for years. The answer is an emphatic “NO” in most cases.

What is true is that .NET make it very easy to write slow code. [tweet]Being more friendly, it is pretty standard for .NET developers being sloppy and uncritical about performance and that, I think, is the main reason why some .NET applications performance sucks.[/tweet]

RavenDB team is doing a great job proving, again and again, that managed code can be so fast as Native Code. Why? Because we pay attention to performance starting from the architecture to the implementation.

When you build your .NET code, the compiler translates it to Intermediate Language and metadata. Then, when you run your code, whenever a method is executed for the first time, the Just in Time Compiler convert it to assembly code. Most of the code optimization happens at this stage (If you want to understand this process, I recommend you to read this post).

The quality of the JITted code is fantastic. Microsoft has improved the JIT, especially quite recently.

In fact, there are advantages in the Managed World:

  1. Since the code is JITted as it is executed, its location in memory is better than that of native code. The related code will be often closer, commonly in a single memory page or the cache. So we have fewer page faults;
  2. Different from Native Code, .NET applications are less susceptible to memory fragmentation and. When it happens, the Garbage Collection compacts the heap solving the problem.
  3. The memory allocation occurs with no contention.

Remember: Garbage Collection in .NET is non-deterministic. But, we can significantly affect how often it runs by controlling the memory allocation patterns, object scope, etc.

 

 

Compartilhe este insight:

Uma resposta

  1. Maybe it would be good to have an example to compare the performance between native (in c, for example) and the same code using .net; despite the fact that I understood the problem is more related to the architecture. (disclaimer: I didn’t see the video yet) I usually see many projects that write pieces of the project natively using the excuse that it is for performance reasons.

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Mais insights para o seu negócio

Veja mais alguns estudos e reflexões que podem gerar alguns insights para o seu negócio:

Neste post, compartilho seis benefícios gerados por um bom projeto de Arquitetura de Software.  Cada um desses benefícios ajuda a...
When you think about Roslyn source code, you should think about performance-oriented design. I would like to share some performance techniques...
A EximiaCo não vende! Quando planejei a empresa decidi que ela não teria um departamento comercial. A estratégia é buscar...
Tentando ser “Júnior” Minha carreira como amador remunerado em programação começou em 1992. Eu tinha quase 13 anos e era...
[tweet]Aprenda comigo técnicas de modelagem de bancos de dados de documentos NoSQL e a utilizar o RavenDB[/tweet] – um dos...
Qual deveria ser o resultado da execução desse código? using System; using System.Threading.Tasks; using static System.Console; using static System.IO.File; class...
× Precisa de ajuda?