从C5通用收集库小型集合相对很慢 - 什么可以做什么? [英] Small sized collections from C5 Generic Collection Library are comparatively very slow - can anything be done?

查看:191
本文介绍了从C5通用收集库小型集合相对很慢 - 什么可以做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在测试在C#C5收藏和我爱它们的功能。对于大型集的表现似乎与一般的同行相提并论。对于小集合但是他们显著慢。我怀疑在相对速度急剧恶化来源于C5藏品进行一定时间的操作。一个操作我知道是射击的事件。难道这是为小集合表现不佳的原因是什么?可这也许通过关闭一些功能关闭来补救?这里的性能测试:

I've recently been testing C5 collections in C# and I'm loving their functionality. For large collections the performance seems on par with the generic counterparts. For small collections however they are significantly slower. I suspect the dramatic deterioration in relative speed comes from constant time operations performed by C5 collections. One operation I know of is firing of events. Could this be the cause of poor performance for small collections? Can this perhaps be remedied by turning some functionality off? Here' the performance test:

//Two containers to be tested. 'Test' is a wrapper over decimal.
var arrayList = new C5.ArrayList<Test>();
var genericList = new System.Collections.Generic.List<Test>();

var toBeAdded = new List<Test>();
var watch = new Stopwatch();

//Fill both tested containers
for (int i = 10; i > 0; i--)
{
    var test = new Test(i);
    genericList.Add(test);
    arrayList.Add(test);
}

//Fill the container the range of which will be inserted to the tested containers
for (int i = 5; i > 0; i--)
{
    toBeAdded.Add(new Test(i+0.5m));
}


//Test the speed of adding a range of values and sorting for both containers
watch.Start();
genericList.AddRange(toBeAdded);
Console.WriteLine("Adding values for generic list: {0} ticks", watch.ElapsedTicks);
watch.Restart();
genericList.Sort();
Console.WriteLine("Sorting for generic list: {0} ticks", watch.ElapsedTicks);

watch.Restart();
arrayList.AddAll(toBeAdded);
Console.WriteLine("Adding values for C5 ArrayList: {0} ticks", watch.ElapsedTicks);
watch.Restart();
arrayList.Sort();
Console.WriteLine("Sorting for C5 ArrayList: {0} ticks", watch.ElapsedTicks);

和测试类:

class Test : IComparable
{
    private decimal _number;
    internal Test(decimal aNumber)
    {
        _number = aNumber;
    }        
    public int CompareTo(object obj)
    {
        var test = (Test) obj;
        return _number.CompareTo(test._number);
    } 
}



输出是:

The output is:

Adding values for generic list: 56 ticks
Sorting for generic list: 770 ticks
Adding values for C5 ArrayList: 3575 ticks
Sorting for C5 ArrayList: 4815 ticks

无论C5和测试的发布版本。 。大约60倍的速度插入和6X进行排序的比例为测试运行之间是一致的。

Both C5 and the test are Release builds. The ratio of speeds of around 60x for inserting and 6x for sorting is consistent between test runs.

编辑:上面的测试是从VS.内跑运行VS之外的结果是:

The above test was ran from within VS. The results for running outside of VS are:

Adding values for generic list: 54 ticks
Sorting for generic list: 2135 ticks
Adding values for C5 ArrayList: 5765 ticks
Sorting for C5 ArrayList: 5198 ticks

再次围绕100X速度的插入和2x排序的比例为测试运行之间是一致的。

Again, the ratio of speeds of around 100x for inserting and 2x for sorting is consistent between test runs.

我的项目包括了很多小的容器操纵的他们的表现是至关重要的。 C5容器的功能是伟大的,我很乐意使用它们,但目前不能为性能的原因。我会很感激任何关于该事项的洞察力

My project includes a lot of manipulating of small containers and their performance is paramount. The functionality of C5 containers is great and I'd love to use them, but at the moment can't for performance reasons. I'd appreciate any insight on the matter.

EDIT2:根据铱星的回答,我在一个循环中执行的测试(把整个逻辑包括容器创建成循环以便排除任何编译器优化技巧),丢弃前两个结果,平均后续1000个结果。在这里,他们是:

As per Iridium answer, I performed the test in a loop (putting the whole logic including the container creation into the the loop so as to exclude any compiler optimization tricks), discarded the first two results and averaged subsequent 1000 results. Here they are:

Adding values for generic list: 1.09 ticks
Sorting for generic list: 14.07 ticks
Adding values for C5 ArrayList: 1.92 ticks
Sorting for C5 ArrayList: 13.69 ticks

现在C5插入慢76%,排序看齐与名单。这对我来说已经足够了。我接受铱星的回答。不过,如果任何人有慢插入任何见解,请分享。感谢所有的帮助。

Now C5 insertion is 76% slower and sorting is on par with that of List. That's good enough for my purpose. I'm accepting Iridium's answer. Still, if anyone has any insight on the slower insertion, please share it. Thanks all for the help.

推荐答案

我在这里想知道,如果你的结果是有点误导,那你的不同重新看到了C5其实(可能)由于组件加载/ JIT编译的第一个使用添加/排序方法的开销。通用集合无法从这种痛苦为已经已经加载/ NGen'ed系统组件的结果。

I'm wondering here if your results are a little misleading, and that the differences you're seeing for C5 are in fact (possibly) due to the overhead of assembly loading/JIT compilation on the first use of the add/sort method. The generic collections not suffering from this as a result of the system assemblies having already been already loaded/NGen'ed.

我重复使用C5 2.1.4596.30350您的测试,但跑整个测试多次(不重新启动该应用程序,以避免任何一次性开销)。结果似乎表明,有第一次使用C5集上后续使用消失(像JIT编译一次性开销一致)的时间损失,更让C5的表现实际上是相同的是,泛型集合的。

I repeated your test using C5 2.1.4596.30350, but ran the whole test multiple times (without restarting the application so as to avoid any one-off overheads). The results seem to suggest that there is an time penalty on first use of C5 collections (consistent with a one-off overhead like JIT compilation) that disappears on subsequent uses, leaving C5 performance effectively the same as that of the generic collections.

这篇关于从C5通用收集库小型集合相对很慢 - 什么可以做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆