I have two fors. When the records are ~ 100, they work quickly, but when the records are more than 100,000, the processing speed can be an hour. How can I speed up this algorithm? I thought to break it into several parallel streams, but then the program may not see the correspondence in different streams, respectively, the repetitions will remain.

HashSet<IP> tbl = new HashSet<IP>(); foreach (IP x in IPs) { if (x.DataValid()) tbl.Add(x); } IPs.Clear(); IPs.AddRange(tbl); for (int i = 0; i < IPs.Count; i++) { for (int j = IPs.Count - 1; j > i; j--) if (IPs[i].ip.Equals(IPs[j].ip)) IPs.RemoveAt(j); int status1 = 100 * i / IPs.Count; Dispatcher.Invoke(() => LabelStatusValue.Text = status1 + "%"); Dispatcher.Invoke(() => progress.Value = status1); } 

Where IPs are an IP class element:

 class IP { public string ip = ""; public string port = ""; public List<string> data = new List<string>(); public override int GetHashCode() { return ip.GetHashCode(); } public bool DataValid() { System.Net.IPAddress address; int port_number = -1; return System.Net.IPAddress.TryParse(ip, out address) && int.TryParse(port, out port_number) && port_number > 0 && port_number < 65536; } } 

Something needs to be done with the two fors. I think you can use Distinct() and use LINQ to get indices of duplicate elements, but I do not have enough knowledge to do this ...

  • and did not try to use List <IP> and Linq functions? - Monomax
  • @Monomax I use the List <IP>, and how to do it Linq lacks knowledge - Dmitry Afteeer
  • IPs = IPs.GrpoupBy(x=>x.ip).Select(x=>x.First()).ToArray(); ? - tym32167
  • one
    Well, updating the UI in a cycle for 100k operations is not very good, it will not add speed. - tym32167
  • Linq Collection.Distinct() remove all repetitions - Sasuke

0