c# - ConcurrentDictionary.Where very slow for filtering based int array (Key field) -
i have following
var links = new concurrentdictionary<int, link>();
which populated around 20k records, have array of strings (list) turn int array using following.
var intpossible = nonexistinglistingids.select(int.parse); //this fast need done
which pretty fast. create new list or filter out "links" in intpossible array matches key element of concurrentdictionary.
i have following using clause takes 50 seconds actual filtering slow want do.
var filtered = links.where(x => intpossible.any(y => y == x.key)).tolist();
i know intersect pretty fast have array of ints , intersect not working against concurrentdictionary
how can filter links little faster instead of 50 seconds.
you need replace o(n) inner lookup more speedy hashset offers o(1) complexity lookups.
so
var intpossible = new hashset<int>(nonexistinglistingids.select(int.parse));
and
var filtered = links.where(x => intpossible.contains(x.key)).tolist();
this avoid iterating of intpossible
every item in links
.
alternatively, linq friend:
var intpossible = nonexistinglistingids.select(int.parse); var filtered = links.join(intpossible, link => link.key, intp => intp, (link, intp) => link);
the implementation of join same thing above.
Comments
Post a Comment