c# - Fast union of two big lists -


i m using union on 2 big lists (over 1 million entries) , s quite slow (a few minutes) need feature remove duplicates cannot use concat , lists not sorted. there faster way? maybe using plinq?

you not saying items in list, 1 option use proper data structure task - want keep unique items - definition of set, use hashset.

var hashset = new hashset<int>(list1); hashset.unionwith(list2); 

also measured time code above vs linq.union:

var list3 = list1.union(list2).distinct(); 

and here timing ( hashset.unionwith works twice faster):

hashset.unionwith real    0m4.111s user    0m3.890s sys 0m0.132s  real    0m4.562s user    0m4.074s sys 0m0.170s  real    0m4.052s user    0m3.851s sys 0m0.129s  real    0m4.003s user    0m3.814s sys 0m0.125s  real    0m4.058s user    0m3.858s sys 0m0.126s   linq.union.distinct real    0m7.579s user    0m7.014s sys 0m0.428s  real    0m7.498s user    0m6.965s sys 0m0.419s  real    0m7.596s user    0m6.994s sys 0m0.412s  real    0m7.446s user    0m6.917s sys 0m0.416s  real    0m7.452s user    0m6.928s sys 0m0.403s 

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -