组合2个Scripting.Dictionaries或Key / Item对的集合 [英] Combine 2 Scripting.Dictionaries or Collections of Key/Item pairs

查看:134
本文介绍了组合2个Scripting.Dictionaries或Key / Item对的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更有效的方法来组合2个字典在VBScript / VBA?我写了下面的函数,但我担心性能的影响:

Is there a more efficient way to combine 2 dictionaries in VBScript / VBA? I wrote the function below, but I'm worried about the performance impact:

Function MergeDicts(Dct1, Dct2)
    'Merge 2 dictionaries. The second dictionary will override the first if they have the same key

    Dim Res, Key

    Set Res = CreateObject("Scripting.Dictionary")

    For Each Key In Dct1.Keys()
        Res.Item(Key) = Dct1(Key)
    Next

    For Each Key In Dct2.Keys()
        Res.Item(Key) = Dct2(Key)
    Next

    Set MergeDicts = Res
End Function


推荐答案

如果您想提高效能,请不要使用晚期绑定: p>

If you'd like to improve performance, don't use late binding :

Dim Res
Set Res = CreateObject("Scripting.Dictionary")

使用早期绑定

添加对 Microsotf脚本运行库库的引用(转到 TOOLS-> REFERENCES

Add reference to Microsotf Scripting Runtime library (go to TOOLS->REFERENCES)

>

然后使用:

Dim Res as New Dictionary

您的算法很好。合并两个字典(每个包含 10000 键/值对)会占用我的机器 0.046875 秒用于早期绑定, 0.069841 用于后期绑定。

Your algorithm is fine. Merging two dictionaries (each contains 10000 key/value pairs) takes on my machine 0.046875 seconds for early binding and 0.069841 for late binding.

这篇关于组合2个Scripting.Dictionaries或Key / Item对的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆