Is there a way to measure boxing / unboxing penalties?

I work with a framework that uses collections derived from System.Collections.CollectionBase . Users complain about performance, and I feel that these collections, which are very heavily used, can be a big part of the problem. Is there a way with a tool or profiler or in IL so that I can get some metrics for boxing / unboxing fines? I need backup evidence for System.Collections.Generic . I tried CLRProfiler, but usually get lost and not sure what I should look for.

UPDATE
Thank you all for your input. I know that this is probably not the main bottleneck, but I am looking for metrics for as many potential performance killers as possible. This is just one of them, not sure how big it is, so it’s looking for a way to measure it.

+4
source share
4 answers

While I certainly recommend that you switch from non-generic to generic collections for a variety of good reasons, I honestly doubt that these collections can cause your performance problems. Boxing, as a rule, is only a problem when you get to the microscopic level, demanding to squeeze out tiny benefits in high-performance situations. It is also useful to avoid general GC considerations, but this is usually negligible in this arena.

In other words: it is very doubtful that boxing will cause a performance problem that your users would notice.

Obviously, I am talking in generalizations. Without knowing your specific scenario, I cannot say so with certainty.


Change Please note that although I am skeptical about your problem, you can use non-general collections, I will indicate that this It is very important what type you use to solve this problem, especially when the amount of data in the collection is large. Here are just a few examples:

  • If you perform a key-based search, a hash table such as Dictionary<TKey, TValue> is significantly superior, for example, to List<T> .
  • If you check for duplicates, HashSet<T> will have excellent performance.
  • If you are looking for FIFO (queuing behavior), Queue<T> will have excellent performance.
  • If you do insert / delete at random positions in a collection, LinkedList<T> will have excellent performance.

These collections should be part of any .NET developer toolkit (indeed, any developer). If you use a List<T> (or ArrayList ) or similar data structure wherever you use item collections, this can very well lead to performance problems in the future - again, especially when your collections are large. I'm not talking about trivial performance indicators. Therefore, be careful to make the smart choice for your types of collections.


But I would recommend a performance profiler in general, such as ANTS (good, but not free) or EQATEC (also good and free). Just run the application as part of a program, such as one of them, and see where your bottlenecks are. I assume that you will find that these are not your non-general collections; but naturally, I could be wrong.

+10
source

Why not just set up a quick console application to measure the speed of various operations. You can use a simple way:

 private TimeSpan TimedAction(Action action) { var timer = new Stopwatch(); timer.Start(); action.Invoke(); timer.Stop(); return timer.Elapsed; } 

And name it as follows:

 var elapsed = TimedAction(() => { //Do some stuff with your collection here }); Console.WriteLine("Elapsed Time: {0}", elapsed.TotalMilliseconds); 

You should be able to collect enough empirical data from this to figure out which collection will be faster with similar operations. The number of elements, the number of operations performed, etc.

However , like Dan mentioned above; the amount of total productivity spent collecting third parties is probably negligible when comparing data access and network latency.

+2
source

Here is the necessary proof for you.

From MSDN :

In addition to security type, generic collection types are usually performed better to store and manipulate value types, because there is no need to enter type values.

Note that in real life, generics are not really as fast as Microsoft says. The difference is negligible.

+1
source

@ Dan Tao's remarks are true for money.

What I find that I do a lot, in similar circumstances, is a method that you can do in any IDE.

So, I know that you want to measure a certain thing, but in general, your biggest problem is finding performance problems, wherever they are, right?

We have discussions about such problems, but the fact that the program really spends time has nothing to do with it. Things like moving 30 layers deep into underground libraries, just to do things like extract strings from resources so that they can be translated into different languages ​​when they don't, in fact, should be. Things like someone set the True property, which selects the notification chain with adding or removing items from lists, updated tree controls, created and destroyed windows, adding / removing tabs and menu items, etc. Then a little later, the property gets False again, as if it doesn't matter much. Things like setting cells in grid control mode where a similar tidal wave occurs. As a rule, dogs wag their tails.

What I mean is what really happens. When things like boxing are a problem, samples will show it.

+1
source

All Articles