VLOOKUP在Datagridviews(VB.NET)Datagridviews、VLOOKUP、NET、VB

2023-09-11 07:19:15 作者:想念已成习惯

我有2 datagridviews(比方说,DGV-A和DGV-B),其只是一个列中的每个。 我想找到是否DGV-A的项目也存在于DGV-B。基本上,我正在寻找在MS-Excel中VLOOKUP函数可用。 这可以平凡完成,公交迭代中DGV-A和DGV-A的每一次迭代值,迭代DGV-B,看它是否存在有(中止DGV-B迭代只要我们找到了项目存在)。 这需要在DGV-A的所有项目要做。而且因为我DGV的可能有大约200个项目(例如,如果DGV包含200个项目每个,在最坏的情况,我会做200 * 200 = 40000 comparisions)在每个datagridviews,恐怕也不会很快。

I have 2 datagridviews (say, DGV-A and DGV-B) having just one column each. I want to find whether the item in DGV-A also exists in DGV-B. Basically, I am looking for a VLookup function available in MS-Excel. This can be done trivially, bus iterating over the values in DGV-A and for each iteration of DGV-A, iterate over DGV-B and see if it exists there (aborting iteration of DGV-B as soon as we have found the item exists). This needs to be done for all the items in DGV-A. And because my DGV's could potentially have around 200 items (e.g. if DGV contain 200 items each, at worst, I would be doing 200*200 = 40000 comparisions) in each datagridviews, I am afraid, it is not going to be quick.

有什么办法/算法来做到这一点的最佳方式。 (我没有任何数据绑定或数据库,因此使用SQL / DB-引擎是不是一种选择,我在DGV的数据编程方式生成的基于用户行为飞)

Is there any way/algorithm to do it in an optimal way. (I do not have any data binding or database, so use of SQL/DB-Engine is not an option; my data in DGV's is generated programmatically on the fly based on the user actions)

推荐答案

这通常不是一个好主意,优化$ C $下的性能,直到你知道,你真的有性能问题。但在这种情况下,我会用另一种解决方案,也避免了 O(N * M)操作。

It is usually not a good idea to optimize code for performance until you know that you really have performance problems. But in this case I would use another solution, too, to avoid an O(n * m) operation.

我建议您插入一个列表中的所有项目到一个哈希集合 - 这将是 O(N)如果你指定一个足够大的初始大小和避免调整散列设置这种方式。然后,只需在第二个列表中的 O(M)执行一个查找在哈希集合每个项目。这样,你得到 O(N * M) O(N + M)

I suggest to insert all items from one list into a hash set - this will be O(n) if you specify a large enough initial size and avoid resizing the hash set this way. Then just perform a look up in the hash set for each item in the second list in O(m). This way you get O(n * m) down to O(n + m).