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?
source share