Is Managed(C#/Java) Code Slower Than Native(C/C++) ?

As a developer, you either ask this question to yourself or keep hearing it from your friends. “Is Managed(C#/Java) Code Slower Than Native(C/C++) ?”. Well the answer is: It depends!

Targeting different CPUs

JIT(Just In Time) Compiler is a major component of managed code. Most of the code you’re running is actually compiled when they’re needed. Which gives JIT Compiler the unique ability of examining the CPU and then enabling CPU specific optimizations. Our buddies in CPU companies are working really hard trying to add new instruction sets making common operations as fast as possible. When you build a native code, you need to target a certain instruction set and it’s not changeable at run time. But again, JIT is compiled at run time which means we’ll spend some CPU time compiling it. As I said, it depends.

Memory Management

Native languages gives you the great power. You can edit any memory you’d like without the need of copying and etc. But do not forget, Great power comes with great responsibility! If you don’t have a strong understanding of native code memory management and ownership, you might start copying objects around unnecessarily which might be a killer for performance. Or even worse, you might introduce memory leaks, buffer overflows, security leaks. Your software might run a bit faster, until it gets shutdown by the operating system with an error message of “Memory Access Violation”! Don’t take this part personally though. I had the opportunity of working with some of the best engineers in the world. And guess what? When the team size is above 50 people, you get memory leaks even with the best engineers on board.

Developer Time

This one is pretty common case all over the web. I felt the need to mention this one since developer time is also a measure in performance. It’s true developer time doesn’t affect the run time of the application but we all are humans and humans tend to have limited time for focusing. The longer time you spend on coding something the lower your attention will be which might reduce the performance of the code written.

Immutable vs Mutable

When managed code was being designed developers had the chance to favor new technologies over old ones. Doesn’t matter how much native code evolves, there is a one main problem called “Backward compatibility”. We can’t go ahead and change main concepts that are well-known by many developers and commonly used today. When native code was designed memory has been chosen as one of the main concerns. Most of the objects are mutable to let users make in-place operations. When managed code appeared, they’ve changed this main concept. For example “strings” in the managed world are immutable. How come this can affect performance ? Easy! In native code every time you call a method and pass a string, you need to know if they will change the string or not. If you’re not sure, you have to copy the string to protect the original. Wait! Can we know who’s touching our string in native code ? What if some class kept the pointer to the string and changes it later in time ? I’m hearing you saying “I’m writing the code and I’m smart enough to catch this!”. Unfortunately not always. Think about how many packages we are working with these days. It takes one wrong design in a package or maybe a bug in them and you’re done. Maybe we should copy them all the time. But that would hurt the performance really bad wouldn’t it ? Immutability in managed code saves you from all the trouble. Nobody can change your string so you can pass them around without the need of copying and rest assured they’re safe. Considering how much space can they take, this can provide huge performance + memory benefits.

Libraries That Comes With Platform

When you use managed code both .Net and Java comes with a lot of packages that’re ready to be used. Do you need to use HTTP ? Do you want to encrypt some input ? Do you need to parse XML or maybe serialize/deserialize and object ? These all come with managed code which saves you the trouble from finding a trustable source for these libraries. Also considering all these technologies developed by the best developers in the world, their performance will be much better than some random library you might find online.

So tell me, Managed or Native ?

As I said in the beginning of my post, it always depends:

  1. If you’re writing your software to run on thousands of servers which has same hardware, native compiler can do an amazing job optimizing performance and you don’t need to wait for JIT(Just In Time) Compiler.
  2. If you’re writing the code yourself or with developers who are really good at native code, the performance will be better(I’ll later share some real numbers on how fast it can be)
  3. If you’re thinking of using a lot of libraries other than some basic ones or
    If you prefer spending your time at a beach instead of making sure there are no memory leaks or
    If you don’t want users to face “Memory Access Violation” error and think you’re malicious or
    If your software is going to be run on different systems or
    You simply want to write software that just runs without any problems, you might want to use Managed Code.

One Response to “Is Managed(C#/Java) Code Slower Than Native(C/C++) ?”

  1. Amin says:

    good job describing it

Leave a Reply

Your email address will not be published. Required fields are marked *