I watch courses on C # proffesional from ITVDN. There is an example, for the first time they refer to the dynamic field, which was previously equivalent to an object reference, and show that the first call to such a field takes 2 million ticks. I repeat it on my computer, but I have only 2 thousand ticks. Approximately 10 times more than the following calls to dynamic. The salt is that dynamic remembers the object, but for the first time it knows nothing about it and therefore takes more time. But 2 thousand and 2 million for the same functions - a very big difference. Could the course be outdated and C # developers optimized dynamic? Code:

MyClass c = new MyClass(); dynamic d = c; long start, end; while (true) { QueryPerformanceCounter(out start); dm(); QueryPerformanceCounter(out end); Console.WriteLine((end - start) + '\n'); } 
  • 6
    I watch courses on C # proffesional from ITVDN. Immediately stop! Video courses - evil. - VladD
  • 2
    and what in this code happens? variable dynamic Not used anywhere else - Grundy
  • In the loop instead of cm () should be dm (). A typo. I corrected, but I did not get the proper effect. Now there are 33 thousand ticks. - kalokusatel
  • 3
    Well, this is a good signal that it’s bad to trust the left video from the Internet. Practice is the criterion of truth. Who knows, maybe these guys for tests used a cold method, and the cost of JIT compilation fell into the calculation? - VladD
  • The fact of the matter is that the method is called unheated (if I understood correctly) to show that the unheated methods take a very long time. And there is. But with about the same results of another code in time, in the video course - 2 million, I have 2 thousand. Probably .NET has been updated, so I wondered if anyone knows. The courses are actually very high quality and detailed. - kalokusatel

1 answer 1

I will not answer the question about the difference in the number of ticks, because neither the measurement method, the configuration of your computer’s author or the author, or the .NET version are known.

Instead, I’ll answer why the first method call is slower. Perhaps someone come in handy. Additions to the post are welcome.

For each expression (operation) that uses an object of type dynamic , the compiler generates a special object called the dynamic call site, which represents this expression (operation). After compiling the code in question, we get something like the following (the code with the dimension dropped):

 static DynamicCallSite dCallSite; ... MyClass c = new MyClass(); dynamic d = c; while (true) { if (dCallSite == null) { dCallSite = new DynamicCallSite(); } dCallSite.DoInvocation("m", d); } 

In this case, the dynamic call site object is generated only once for each expression. This is the first coin in the piggy bank "why the first call is slower."

Further, the DLR checks the type of the object d , detects that it is a C # object, and calls the C # compiler, which, using metadata, generates an expression tree for the given expression. The Expression tree is returned back to the DLR, compiled, the resulting delegate is called and cached . Those. if the DLR subsequently encounters the same expression for an object of the same type, the cached delegate will be called. This is the second coin in the piggy bank "why the first call is slower."

During the second call, we already have a dynamic call site, so the DLR simply checks the type of the object, gets the compiled delegate from the cache and executes it.