我怎么能增加C#性能的实时 [英] How I can increase C# performance to real time

查看:108
本文介绍了我怎么能增加C#性能的实时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的数据量,我需要用C#
来计算它,我有10000患者DB一位患者在他的纪录DB
的平均30次,我需要检查自己对于他的访问我已通过LINQ SQL组合做了组合,在C#并与其他病人$ b $比较b如果我发现4例具有相同的组合,我会考虑它的安全

I have a huge amount of data that i need to compute it by C# I have 10000 patients on DB and every patient has an average 30 visits on his record on DB I need to check his combination for his visits which i have done it via LINQ SQL combinations in C# and compare it with other patient if i found 4 other patients has the same combination i will consider it safe

我用这个循环代码,但需要时间,我需要提高性能。

I used this for loop code but take times and I need to increase the performance

Parallel.For(0, 10000, (j,loopState) =>
        {
            int id1 = getid(names[j].ToString(), 101);
            Parallel.For(0, 10000, (t,loopState1) =>
                {
                    generate_firstACV5A(id1, nvisit(id1), names[j].ToString());
                    int id2 = getid(names[t].ToString(), 10000);
                    if (id1 == id2) { }
                    else
                    {
                        generate_firstACV5B(id2, nvisit(id2), names[t].ToString());
                    }

                    if (Enumerable.SequenceEqual(ACV5BFirst, ACV5Afirst) == true)
                    {
                        if (count == 0)
                     {
                         safecount++;
                         if (safecount == 4) {
                             ListViewItem it = new ListViewItem(getid(names[j].ToString(), 1000).ToString());
                             it.SubItems.Add(names[j].ToString());
                             listView3.Items.Add(it); loopState1.Break();
                         }


                       }
                    }
                });



我得到了一些运行错误,当我比较名单是真实的。

I got some run error when i compare the list to be true

我感谢你的帮助。

推荐答案

如果你至少有几百个了 ListViewItems 由代码生成的一个问题是,您要添加 ListViewItems 中的代码执行,触发在的ListView 重绘(相对于所有其他计算你这是慢)。把你的 ListViewItems 列表第一,当你完成,你加他们为阵,这样的:

If you have at least few hundreds of the ListViewItems generated by your code, one of the problems is that you are adding ListViewItems during the code execution which triggers the ListView redrawing (which is slow compared to all other computing you have). Put your ListViewItems in the List first and when you finish, you add them as array, like this:

List<ListViewItems> list = new List<ListViewItems>();
Object myLock = new Object();
Parallel.For(0, 10000, (j,loopState) =>
        {
            int id1 = getid(names[j].ToString(), 101);
            Parallel.For(0, 10000, (t,loopState1) =>
                {
                    generate_firstACV5A(id1, nvisit(id1), names[j].ToString());
                    int id2 = getid(names[t].ToString(), 10000);
                    if (id1 == id2) { }
                    else
                    {
                        generate_firstACV5B(id2, nvisit(id2), names[t].ToString());
                    }

                    if (Enumerable.SequenceEqual(ACV5BFirst, ACV5Afirst) == true)
                    {
                        if (count == 0)
                     {
                         safecount++;
                         if (safecount == 4) {
                             ListViewItem it = new ListViewItem(getid(names[j].ToString(), 1000).ToString());
                             it.SubItems.Add(names[j].ToString());

                             lock (myLock )
                             {
                                 list.Add(it); 
                             }

                             loopState1.Break();
                         }


                       }
                    }
                });
listView3.Items.AddRange(list.ToArray());



即使锁定访问添加在列表时,会再与约1000重绘ListView的快得多项目。当我测试,这种方法已经接近快10倍。

Even when locking the access to the list during Add will be much faster then redrawing the listView with approximately 1000 items. When I tested, this approach had been near 10 times faster.

这篇关于我怎么能增加C#性能的实时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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