Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to measure boxing/unboxing penalties?

I'm working with a framework that uses collections derived from System.Collections.CollectionBase. Users have been complaining about performance, and I feel that these collections, which are very heavily used, might be a big part of the problem. Is there a way with a tool or profiler or in the IL that I can get some metrics on boxing/unboxing penalties? I need evidence to back-up a push for System.Collections.Generic instead. I've tried CLRProfiler, but tend to get lost and am not sure what I should be looking for.

UPDATE
Thanks all for your input so far. I am aware that this is probably not the main bottleneck, but am looking for metrics on as many possible performance killers as I can. This is just one of them, not sure how big it is, hence looking for a way to measure it.

like image 696
AJ. Avatar asked Dec 10 '22 12:12

AJ.


2 Answers

While I certainly encourage you to move from non-generic to generic collections for plenty of good reasons, I honestly doubt that these collections could be the cause of your performance problems. Boxing is generally only an issue when you get to the microscopic level, needing to squeeze out tiny gains in high-performance situations. It's also good to avoid in general for GC reasons, but it's typically minor in that arena as well.

To put it another way: it's highly doubtful that boxing would cause a performance issue that your users would notice.

Obviously, I'm speaking in generalizations. Without knowing your specific scenario, I can't really say that much with any certainty.


Edit: Note that while I am skeptical your problem could be your use of non-generic collections per se, I will point out that it is very important what type of collection you use to tackle a given problem, particularly when the amount of data in the collection is large. Here are just a few examples:

  • If you are performing lookups based on a key, a hash table such as Dictionary<TKey, TValue> will significantly outperform a List<T>, for example.
  • If you are checking for duplicates, a HashSet<T> will have superior performance.
  • If you are looking for FIFO (queue-like) behavior, a Queue<T> will have superior performance.
  • If you are performing insertions/removals at random positions within the collection, a LinkedList<T> will have superior performance.

These collections should be part of any .NET developer's (really, any developer's) set of tools. If you find yourself using a List<T> (or ArrayList) or similar data structure everywhere you utilize collections of items, that very well may cause a performance issue down the road--again, particularly when your collections are large. These are not trivial performance gains I'm talking about. So do take care to make sensible choices for your collection types.


But I'd recommend a performance profiler in general, such as ANTS (good, but not free) or EQATEC (also good and free). Just run your application under a program such as one of these and see where your bottlenecks are. My guess is that you'll find it isn't with your non-generic collections; but naturally, I could be wrong.

like image 69
Dan Tao Avatar answered Dec 23 '22 22:12

Dan Tao


Why not just set up a quick console app to measure the speeds of various operations. You could use a simple method like this:

private TimeSpan TimedAction(Action action)
{
    var timer = new Stopwatch();

    timer.Start();

    action.Invoke();

    timer.Stop();

    return timer.Elapsed;
}

And call it like this:

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 evidence from this to figure out which collection would be faster given analogous operations. Number of items, number of contiguous operations performed, etc...

However, as Dan mentioned above; the amount of overall performance spent on the third party collection is probably insignificant when juxtapose to data access and network latency.

like image 28
Josh Avatar answered Dec 24 '22 00:12

Josh