C # 'as' vs 'is' + execution efficiency

I read Jon Skeet's second word "C # in depth" when I came across a paragraph in the chapter "Nullable types":

JUDICIAL PROGRAM OF WORK I always thought that performing one test would be faster than two, but it doesn't seem to be like that - at least with the .NET versions I tested with. When writing a quick test that summed all the integers in an array of type "object []", where only a third of the values โ€‹โ€‹were actually integers in the box using "is", and then the result ends 20 times faster than using the 'like' operator "

At first glance, I thought that maybe the compiler is smart enough to optimize double-type checking when using 'is' + cast, but it seems that it is not. The .NET 4.0 code compiled with the 32-bit compiler with the / OPTIMIZE flag enabled still led to two MSIL calls 'isinst'.

What could be the explanation for such a dramatic variation in performance for a case that intuitively should be the other way around?

+4
source share

Source: https://habr.com/ru/post/1481723/


All Articles