Posts Tagged ‘With’

With Slows Down Code?

Tuesday, June 30th, 2009

I just remembered something that happened a few years back in .NET 1.1 days (or maybe even it was .NET 1.0).

We were writing a rather complicated piece of code that needed to do lots of loops processing objects. Since it was taking an extremely long time, we ran it through a code profiler (a nice thing called ANTS Profiler – made by a company called Red Gate). One of the things that the profiler highlighted was that when we were using a With block to access a the return value of a function, every time the runtime used the With block to access the value, it was calling the function and taking extra time! This was a little surprising, as we had thought that With was dealt with at compile time, but apparently not. If my memory serves me correctly, I think that it was still slow when we assigned the return value of the function to a variable and used With to access that.

The basic upshot is that if you are in a situation where you are really bothered about speed, don’t use With to write code like that.