1

以下のステートメントは、SecurityInfoMasterList に約 11,000 項目があり、listClassiNode に約 750 項目がある場合、出力を生成するのに約 6 秒かかります。

同じ結果を達成するためにこれを行う他の方法はありますが、パフォーマンスは向上しますか?

List<SecurityInfo> listSecurityInfo = SecurityInfoMasterList.Where(c => 
    listClassiNode.Any(d => 
        c.SX == d.Exch && c.Instrument == d.Instrument)).ToList();

for ループを使用しようとしましたが、あまり改善が見られませんでした。

更新しました:

listClassiNode はリストです

[Serializable]
     public class SecurityInfo
    {
    public string SecurityID { get; set; }
    public int SecurityTypeID { get; set; }
    public string Code { get; set; }
    public string SecurityName { get; set; }
    public int DB { get; set; }
    public string ExchangeName { get; set; }
    public DateTime FirstDate { get; set; }
    public int StatusCode { get; set; }
    public long GICS { get; set; }
    public string ICB { get; set; }
    public string Sector { get; set; }
    public string IndustryGroup { get; set; }
    public string Industry { get; set; }
    public string Instrument { get; set; }
    public string TypeDescription { get; set; }
    public string SX { get; set; }
    public string GUID { get; set; }
  }



[Serializable()]
    public class ClassificationNode
    {
        public string Exch { get; set; }
        public string Instrument { get; set; }
        public string Prefix { get; set; }
        public string Name { get; set; }
        public string Level { get; set; }
    }

アラン

4

3 に答える 3

3

Parallel を使用してみて、役立つかどうかを確認できます

List<SecurityInfo> listSecurityInfo = SecurityInfoMasterList.AsParallel.Where(c => 
    listClassiNode.Any(d => 
        c.SX == d.Exch && c.Instrument == d.Instrument)).ToList();
于 2013-09-05T02:23:18.530 に答える
3

あなたはあなたlistClassiNodeを何らかの種類に変換することができHashSetます.O(1)O(n)

var hash = new HashSet<string>(
    listClassiNode.Select(t => 
        string.Format("{0}_{1}", t.Exch, t.Instrument)).Distinct());

List<SecurityInfo> listSecurityInfo = SecurityInfoMasterList.Where(c => 
    hash.Contains(string.Format("{0}_{1}", c.SX, c.Instrument))
        .ToList();

上記はstring.Format、HashSet に使用する連結キーを作成するため、少し不器用です。データの性質上、問題にならないことを願っています。とにかく、あなたはアイデアを得ると思います。

于 2013-09-05T02:35:25.470 に答える
0

あなたのクラスを使用して

DEBUG モードでの実行には約 4 ~ 5 秒かかります。

11,000 x 750 の代わりに 12,000 x 12,000

 class Program
    {
        static void Main(string[] args)
        {
            var listSecurityInfo = new List<SecurityInfo>();
            var listClassiNode = new List<ClassiNode>();

            initList(listSecurityInfo, listClassiNode);

            var sw = System.Diagnostics.Stopwatch.StartNew();
            var matched = listSecurityInfo.Where(c => listClassiNode.Any(d =>  c.SX == d.Exch && c.Instrument == d.Instrument)).ToList();
            sw.Stop();

            Console.WriteLine("took: " + sw.ElapsedMilliseconds + "ms matched: " +matched.Count());
            Console.Read();
        }

        private static void initList(List<SecurityInfo> listSecurityInfo, List<ClassiNode> listClassiNode)
        {
            var rnd = new Random();

            for (int i = 0; i < 12000; ++i)
            {
                listSecurityInfo.Add(new SecurityInfo()
                {
                    SX = new string(Convert.ToChar(rnd.Next(40, 125)), 4000),
                    Instrument = new string(Convert.ToChar(rnd.Next(40, 125)), 4000)
                });
            }

            for (int i = 0; i < 12000; ++i)
            {
                listClassiNode.Add(new ClassiNode()
                {
                    Exch = new string(Convert.ToChar(rnd.Next(40, 125)), 4000),
                    Instrument = new string(Convert.ToChar(rnd.Next(40, 125)), 4000)
                });
            }
        }
    }
于 2013-09-05T02:32:07.277 に答える