Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In managed code, what should I look after to keep good performance?

I am originally a native C++ programmer, in C++ every process in your program is bound to your code, i.e, nothing happens unless you want it to happen. And every bit of memory is allocated (and deallocated) according to what you wrote. So, performance is all your responsibility, if you do good, you get great performance.

(Note: Please don't complain about the code one haven't written himself such as STL, it's a C++ unmanaged code after all, that is the significant part).

But in managed code, such as code in Java and C#, you don't control every process, and memory is "hidden", or not under your control, to some extent. And that makes performance something relatively unknown, mostly you fear bad performance.

So my question is: What issues and Bold Lines should I look after and keep in mind to achieve a good performance in managed code?

I could think only of some practices such as:

  • Being aware of boxing and unboxing.
  • Choosing the correct Collection that best suites your needs and has the lowest operation cost.

But these never seem to be enough and even convincing! In fact perhaps I shouldn't have mentioned them.

Please note I am not asking for a C++ VS C# (or Java) code comparing, I just mentioned C++ to explain the problem.

like image 216
Tamer Shlash Avatar asked Jan 08 '12 18:01

Tamer Shlash


People also ask

What is managed code in CLR?

To put it very simply, managed code is just that: code whose execution is managed by a runtime. In this case, the runtime in question is called the Common Language Runtime or CLR, regardless of the implementation (for example, Mono, . NET Framework, or . NET Core/.

What is managed code and unmanaged code in C# with example?

A code which is written to aimed to get the services of the managed runtime environment execution like CLR(Common Language Runtime) in . NET Framework is known as Managed Code. It always implemented by the managed runtime environment instead of directly executed by the operating system.

What is managed code and unmanaged code?

Managed code is the one that is executed by the CLR of the . NET framework while unmanaged or unsafe code is executed by the operating system. The managed code provides security to the code while undamaged code creates security threats.

Is C++ managed code?

Managed C++ is the only language that can contain unmanaged code and natively communicate with all other . NET languages. Managed C++ is thus very convenient for interoperability between programmers who use different languages, including those in the . NET theater and those who use standard C++.


3 Answers

There is no single answer here. The only way to answer this is: profile. Measure early and often. The bottlenecks are usually not where you expect them. Optimize the things that actually hurt. We use mvc-mini-profiler for this, but any similar tool will work.

You seem to be focusing on GC; now, that can sometimes be an issue, but usually only in specific cases; for the majority of systems the generational GC works great.

Obviously external resources will be slow; caching may be critical: in odd scenarios with very-long-lived data there are tricks you can do with structs to avoid long GEN-2 collects; serialization (files, network, etc), materialization (ORM), or just bad collection/algorithn choice may be the biggest issue - you cannot know until you measure.


Two things though:

  • make sure you understand what IDisposable and "using" mean
  • don't concatenate strings in loops; mass concatenation is the job of StringBuilder
like image 168
Marc Gravell Avatar answered Oct 16 '22 02:10

Marc Gravell


Reusing large objects is very important in my experience.

Objects on the large object heap are implicitly generation 2, and thus require a full GC to clean up. And that's expensive.

like image 39
CodesInChaos Avatar answered Oct 16 '22 01:10

CodesInChaos


The main thing to keep in mind with performance with managed languages is that your code can change structure at runtime to be better optimized.

For example the default JVM most people use is Sun's Hotspot VM which will actually optimize your code as it runs by converting parts of the program to native code, in-lining on the fly and other optimizations (such as the CLR or other managed runtimes) which you will never get using C++. Additionally Hotspot will also detect which parts of you're code are used the most and optimize accordingly. So as you can see optimising performance on a managed system is slightly harder than on an un-managed system because you have an intermediate layer that can make code faster without your intervention.

I am going to invoke the law of premature optimization here and say that you should first create the correct solution then, if performance becomes an issue, go back and measure what is actually slow before attempting to optimize.

like image 37
ahjmorton Avatar answered Oct 16 '22 00:10

ahjmorton