你能试着提高这个列表操作的性能吗。[英] Can you try improving performance of this list operation.

本文是小编为大家收集整理的关于你能试着提高这个列表操作的性能吗。的处理方法,想解了你能试着提高这个列表操作的性能吗。的问题怎么解决?你能试着提高这个列表操作的性能吗。问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

您能否尝试提高此列表操作的性能.你会很容易理解我想要达到的目标.

但基本上有一个文件列表和一个与该文件关联的块列表.块可能是重复的(块的主键是 FileId 和 Start).所以我想要另一个文件列表,其中只有最后添加到块列表中的块.


使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Text;使用 System.Diagnostics;使用 System.Collections.ObjectModel;使用 System.IO;命名空间 ListPerformance{class 程序{static void Main(string[] args){随机 rnd = new Random();int fileCount = 1000;int chunkCount = 1000;秒表 w = new Stopwatch();w.Start();列出<hvfile>files = new List();列出<hvchunk>chunks = new List<hvchunk>();for (int i = 0; i< fileCount; i++){int fileId = rnd.Next();files.Add(new HVFile() { FileId = fileId });int chunksToAdd = rnd.Next(chunkCount);for (int j = 0; j< chunksToAdd; j++){chunks.Add(new HVChunk() { FileId = fileId, Start = rnd.Next(chunksToAdd) });chunks.Add(new HVChunk() { FileId = fileId, Start = rnd.Next(chunksToAdd) });chunks.Add(new HVChunk() { FileId = fileId, Start = rnd.Next(chunksToAdd) });chunks.Add(new HVChunk() { FileId = fileId, Start = rnd.Next(chunksToAdd) });}}Console.WriteLine("总文件数={0},总块数={1}",文件.计数,块.计数);Console.WriteLine("列表创建耗时:{0}", w.Elapsed);Console.WriteLine("======== 生成新字典 ============ \n");w.Restart();字典<int,>>fileChunks = new 字典<int,>>();for (int i = 0; i< chunks.Count; i++){HVChunk c = 块[i];if (!fileChunks.ContainsKey(c.FileId))fileChunks.Add(c.FileId, new List<hvchunk>());HVChunk newChunk = fileChunks[c.FileId].FirstOrDefault(x => x.FileId == c.FileId && x.Start == c.Start);if (newChunk != null){fileChunks[c.FileId].Remove(newChunk);文件块[c.FileId].Add(c);}其他{fileChunks[c.FileId].Add(chunks[i]);}}//foreach (var kv in fileChunks)// Console.WriteLine("FileId={0}, chunkCount={1}", kv.Key, kv.值.Count);Console.WriteLine("字典中的文件总数 = {0}", fileChunks.Count);Console.WriteLine("创建字典所用时间 = {0} \n", w.Elapsed);w.Restart();Console.WriteLine("======== 操作完成 ============");//Console.Read();string oldLog = File.ReadAllText("log.txt").ToString();string log = oldLog + Environment.NewLine + "文件总数: " + fileChunks.Count + " ; 总块数: " + chunks.Count +" ; 总时间:" + w.Elapsed;File.WriteAllText("log.txt", log);}}public class HVFile{public int FileId { get;设置;}}public class HVChunk{public int FileId { get;设置;}public int 开始 { get;设置;}}}

推荐答案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.IO;

namespace ListPerformance
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random();
            int fileCount = 1000; int chunkCount = 1000;
            Stopwatch w = new Stopwatch(); w.Start();
            List<HVFile> files = new List<HVFile>();
            List<HVChunk> chunks = new List<HVChunk>();
            for (int i = 0; i < fileCount; i++)
            {
                int fileId = rnd.Next();
                files.Add(new HVFile() { FileId = fileId });
                int chunksToAdd = rnd.Next(chunkCount);
                for (int j = 0; j < chunksToAdd; j++)
                {
                    chunks.Add(new HVChunk() { FileId = fileId, Start = rnd.Next(chunksToAdd) });
                    chunks.Add(new HVChunk() { FileId = fileId, Start = rnd.Next(chunksToAdd) });
                    chunks.Add(new HVChunk() { FileId = fileId, Start = rnd.Next(chunksToAdd) });
                    chunks.Add(new HVChunk() { FileId = fileId, Start = rnd.Next(chunksToAdd) });
                }
            }

            Console.WriteLine("Total file Count={0}, Total chunks Count={1}", files.Count, chunks.Count);
            Console.WriteLine("List creation took: {0}", w.Elapsed);
            Console.WriteLine("========  Generating New Dictionary ============  \n"); w.Restart();

            // Method 1
            Dictionary<int, List<HVChunk>> fileChunks = new Dictionary<int, List<HVChunk>>();


            for (int i = 0; i < chunks.Count; i++)
            {

                if (!fileChunks.ContainsKey(chunks[i].FileId))
                    fileChunks.Add(chunks[i].FileId, new List<HVChunk>());

                fileChunks[chunks[i].FileId].Add(chunks[i]);

            }
            List<int> keys = new List<int>(fileChunks.Keys);

            foreach (int pair in keys)
                fileChunks[pair] = fileChunks[pair].Distinct().ToList();



            Console.WriteLine("Total files in the dictionary = {0}", fileChunks.Count);
            Console.WriteLine("Time taken to Create Dictionary = {0} \n", w.Elapsed);
            Console.WriteLine("========  Operation 1 Complete ============");

            w.Restart();

            // Method 2
            Dictionary<int, List<HVChunk>> fileChunks1 = new Dictionary<int, List<HVChunk>>();
            for (int i = 0; i < chunks.Count; i++)
            {
                HVChunk c = chunks[i];
                if (!fileChunks1.ContainsKey(c.FileId))
                    fileChunks1.Add(c.FileId, new List<HVChunk>());
                HVChunk newChunk = fileChunks1[c.FileId].FirstOrDefault(x => x.FileId == c.FileId && x.Start == c.Start);
                if (newChunk != null)
                {
                    fileChunks1[c.FileId].Remove(newChunk);
                    fileChunks1[c.FileId].Add(c);
                }
                else
                {
                    fileChunks1[c.FileId].Add(chunks[i]);
                }
            }

            Console.WriteLine("Total files in the dictionary = {0}", fileChunks1.Count);
            Console.WriteLine("Time taken to Create Dictionary = {0} \n", w.Elapsed); w.Restart();
            Console.WriteLine("========  Operation 2 Complete ============");


            if (fileChunks1.Keys.Count == fileChunks.Keys.Count)
            {
                List<int> allKeys = new List<int>(fileChunks.Keys);

                foreach (int pair in allKeys)
                {
                    if (fileChunks1[pair].Count != fileChunks[pair].Count)
                    {
                        Console.WriteLine("========  BOTH DICTIONARY ARE NOT EQUAL ============");
                        break;
                    }
                }
            }

            Console.WriteLine("========  BOTH DICTIONARY ARE EQUAL ============");
        }

    }

    public class HVFile
    {
        public int FileId { get; set; }
    }

    public class HVChunk : IEquatable<HVChunk>
    {
        public int FileId { get; set; }
        public int Start { get; set; }

        public bool Equals(HVChunk other)
        {
            //Check whether the compared object is null. 
            if (Object.ReferenceEquals(other, null)) return false;

            //Check whether the compared object references the same data. 
            if (Object.ReferenceEquals(this, other)) return true;

            //Check whether the HVChunk'' properties are equal. 
            return FileId.Equals(other.FileId) && Start.Equals(other.Start);
        }

        // If Equals() returns true for a pair of objects  
        // then GetHashCode() must return the same value for these objects. 

        public override int GetHashCode()
        {
            int hashFileId = FileId.GetHashCode();
            int hashStart = Start.GetHashCode();
            return hashFileId ^ hashStart;
        }
    }
}


Total file Count=1000, Total chunks Count=1956352
List creation took: 00:00:00.2618299
========  Generating New Dictionary ============

Total files in the dictionary = 999
Time taken to Create Dictionary = 00:00:00.4972550

========  Operation 1 Complete ============
Total files in the dictionary = 999
Time taken to Create Dictionary = 00:00:35.8820654

========  Operation 2 Complete ============
========  BOTH DICTIONARY ARE EQUAL ============
Press any key to continue . . .


方法 1[00:00:00.4972550] 是我修改过的方法,方法 2[00:00:35.8820654] 是你原来的方法.请验证输出字典.我有验证代码,它只是比较 fileId 和块.

本文地址:https://www.itbaoku.cn/post/1355916.html