0

I just discovered a weird behaviour in my application while doing a perfomance analysis with ANTS Performance Profiler:

public void set_SelectedObject(object value)
{
    if (value == null) //65ms
    {
       //do anything
    }
}

This check takes 65ms whereas other checks if objects are null take less than 0,Xms. What could be the reason for this? I thought a null-check is always constant no matter what value is passed - Does it depend on the size of my object?

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
Frame91
  • 3,670
  • 8
  • 45
  • 89
  • It shouldn't. I think either that's not your actual code, or you're misinterpreting the profiler's results. Are you sure it's not the *whole* method that's taking 65ms to execute? – dcastro Jul 10 '14 at 10:05
  • What is the hit count? Might be a huge number? – Sriram Sakthivel Jul 10 '14 at 10:06
  • Thanks for your answers. What I let out was the fact, that this method is "generated" by Reflector. The class is a Grid in DevExpress. Might it be that the reflector didn't catch a PropertyChangedEvent or some other dependencies? And the hit count is 4, but I already divided the time through this value (in fact it was 258ms in total) – Frame91 Jul 10 '14 at 10:09

2 Answers2

2

It's nonsense. Checking the value against null will always have similar impact on your performance. It may take 65 ms, because a reference you're sending to the method may actually be a null, which triggers the logic inside your if statement or the oposite - there's some heavy logic that's being fired, when the object is not a null.

The only theoretical reason I could imagine is that you use an overloaded == operator for some class, but it'd have to be really poor piece of code if it wouldn't check for null first.

Tarec
  • 3,268
  • 4
  • 30
  • 47
  • Actually my method is "generated" by Reflector. The original method is "Set_Selected_Object" in DevExpress-XtraVerticalGrid. I guess they fire an event if the value is set... – Frame91 Jul 10 '14 at 10:12
  • 3
    Your theoretical reason is not true. `==` operator can't be overloaded for `System.Object` – Sriram Sakthivel Jul 10 '14 at 10:12
  • @SriramSakthivel You are right. The operator'd have to be a virtual method, which obviously won't be implemented this way, since it's static. – Tarec Jul 10 '14 at 10:18
2

The cast-iron rule for profilers is that absolute results aren't accurate or important. They are only useful as a comparison tool - i.e. is my code faster with or without change X? That said, 65ms is still a substantial chunk of time that shouldn't appear as a result of profiler variance, unless the profiler is really bad.

I haven't used the C# ANTS profiler, I would be amazed if it gave you a timing for single line of code like that. Are you sure it's not the time to execute the entire block surrounded by the if-statement?

If it is giving you a time for just that line, that implies a function call is being made - i.e. an operator overload on the value class.

Airsource Ltd
  • 32,379
  • 13
  • 71
  • 75
  • you're right, the time is shown in the line of the equasion, but I guess this is showing the time for the whole if-else statement... – Frame91 Jul 10 '14 at 10:14