我需要快速更换System.Collections.Generic.Dictionary<TKey, TValue>.我的申请应该非常快.所以,替换应该支持:
......就是这样.我不需要LINQ或任何支持.它应该很快.
一个简单的代码如:
Stopwatch stopWatch = Stopwatch.StartNew();
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("fieldName", "fieldValue");
dictionary.Add("Title", "fieldVaaaaaaaaaaaaaaaaalue");
Console.WriteLine(stopWatch.Elapsed);
Run Code Online (Sandbox Code Playgroud)
...打印00:00:00.0001274,这对我来说很多时间,因为我的应用程序正在做很多其他的事情,其中一些来自旧的慢速库我必须使用并且不依赖于我.
关于如何实施更快速的想法?
谢谢.
我正在做一个性能关键程序(很少学术),我正在寻求尽可能优化(不像它证明"这是"瓶颈).
我有一个自定义字典结构(.NET的包装Dictionary<,>),我会不断删除一个阶段的项目(按Key值).我需要Value删除的项目.现在我必须这样做:
T t;
if !TryGet(key, out t)
return false;
Remove(key);
Run Code Online (Sandbox Code Playgroud)
这是两次查找.我会喜欢这个:
public bool Remove(S key, out T value)
{
// implementation
}
Run Code Online (Sandbox Code Playgroud)
我知道框架中没有任何内容,但是某处有实现吗?如果是这样的话,我会用那个更改我的支持词典.
编辑:嗯,我知道这两个TryGetValue和Remove是O(1).只知道是否有任何集合结构只能在一次查找中产生相同的效果.正如我所说,我正在努力尽可能地优化.只是知道.
我想知道是否可以IDictionary通过其键删除项目,同时获取已删除的实际值?
就像是:
Dictionary<string,string> myDic = new Dictionary<string,string>();
myDic["key1"] = "value1";
string removed;
if (nameValues.Remove("key1", out removed)) //No overload for this...
{
Console.WriteLine($"We have just remove {removed}");
}
Run Code Online (Sandbox Code Playgroud)
//We have just remove value1
Run Code Online (Sandbox Code Playgroud)