1

Whats the difference between the teo snippets?

Snippet 1:

{
  Dictionary<MyCLass, bool> dic;
  MyFunc(out dic);
}

Snippet 2:

{
  Dictionary<MyCLass, bool> dic = null;
  MyFunc(out dic);
}

Is snippet 2 better in performance?

FIre Panda
  • 6,537
  • 2
  • 25
  • 38

4 Answers4

4

Technically speaking the second code snippet will likely execute more instructions than the first by doing a redundant null set. I'm hedging with likely here because the C# spec may allow for the flexibility of ignoring this set. I don't know off hand.

However I would seriously doubt that would ever noticeably affect performance of an application. I certainly would not code for that optimization but would instead prefer the solution which I found more understandable.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    @Abdul personally I would prefer the first as the null set in the second is redundant. – JaredPar May 13 '11 at 06:49
  • What do you mean by redundant null set? – FIre Panda May 13 '11 at 06:51
  • @Abdul the first action after declaration is to pass `dic` as an `out` parameter. This means the code inside `MyFunc` can't access the existing value without first writing to it. Hence neither the original function or `MyFunc` can see the `null` value and it's redundant. – JaredPar May 13 '11 at 06:52
  • Is there a performance overhead of creating dictionary quite a lot? – FIre Panda May 13 '11 at 06:54
  • 1
    @Abdul The second code snippet won't really affect your performance. Just stopy worrying about it, you probably won't even be able to measure the possible performance loss. – Christian May 13 '11 at 06:58
  • @Abdul performance questions are **extremely** difficult to give definitive answers for because it's a very context sensitive question. There is definitely a performance *impact* for creating many instances of Dicitonary (or any other type). But this may or may not be significant to your application and the only way to tell is to use a profiler. – JaredPar May 13 '11 at 06:58
  • I have used profiler and it says Dictionary dic; time 1361 sec hit count 47381. Isnt 1361 sec quite a lot? – FIre Panda May 13 '11 at 07:00
  • @Abdul, it's still contextual. If the 1361 seconds profiled over a 1 week run of the application I'd say probably not that important, but if it were done over say a much shorter time (hour or so) then yes it might be important. – JaredPar May 13 '11 at 07:04
  • yeah in a hour or so, not more than an hour, any suggestion about that? – FIre Panda May 13 '11 at 07:05
  • @Abdul if it is less than an hour and you're spending over 30% of the time allocating dictionaries so I'd rexamine why I needed to allocate that many and could I implement some bit of re-use – JaredPar May 13 '11 at 07:07
1

I like snippet 2, it's slower but better practice to reduce errors, overall a good habit to have - to init variables explicitly. Maybe even the JIT can optimize it away at access time so you only lose a little bit of performance at compile & load time not at execution (but I haven't verified this debugger/disassembler but the JIT is quite 'smart' for a computer program so it maybe able to do it)

misosim
  • 11
  • 3
1

Do not worry about these when you haven't measured the performance of the application.

Things like this are very unlikely to have a huge impact, in fact, most of the time things like this will not be noticeable compared to other lines you wrote.

Measure first, them worry about performance.

Emond
  • 50,210
  • 11
  • 84
  • 115
0

Compile them both and compare the IL. I imagine it would be the same. The storage for the out parameter should be initialized to zero (null, if it is a reference type) before it its passed to the called method.

phoog
  • 42,068
  • 6
  • 79
  • 117