Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does braces take time on C# code?

I am using Ants Performance Profiler 8.5 and when I saw the time each row of my code runs, I have noticed that the braces also take time.

Here is an image, you can see at the left of the braces the time in milliseconds:

enter image description here

Sometimes I get more time, like 5 ms...

Why is that? Is it the garbage collection?

like image 556
Misha Zaslavsky Avatar asked Jun 02 '14 06:06

Misha Zaslavsky


1 Answers

When a method is defined, the set of parameters that are in scope for the method are known by the compiler, called a maxstack. This hints at the amount of memory to allocate for the method.

This could be the source of the extra time - memory allocation by the CLR.

Adding more braces doesn't actually add more parameters to maxstack. Its scopes the whole method. Scope is more of a logical grouping, than being implemented by the CLR to release memory.

With your question on GC, I don't believe this is the root-cause of the problem. GC is ran by a separate thread when required. It could be the GC, but I seriously doubt it in your case.

like image 153
Dominic Zukiewicz Avatar answered Oct 15 '22 14:10

Dominic Zukiewicz