更快的更换,以字典< TKEY的,TValue> [英] A faster replacement to the Dictionary<TKey, TValue>

查看:150
本文介绍了更快的更换,以字典< TKEY的,TValue>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个快速替换 System.Collections.Generic.Dictionary< TKEY的,TValue> 。我的应用程序应该是真的快。因此,更换应支持:

I need a fast replacement for the System.Collections.Generic.Dictionary<TKey, TValue>. My application should be really fast. So, the replacement should support:

  • 泛型
  • 添加
  • 获取
  • 包含

......,仅此而已。我不需要LINQ或任何的支持。它应该是

... and that's it. I don't need any support in LINQ or anything. And it should be fast.

一个简单的code这样的:

A simple code like:

Stopwatch stopWatch = Stopwatch.StartNew();

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("fieldName", "fieldValue");
dictionary.Add("Title", "fieldVaaaaaaaaaaaaaaaaalue");

Console.WriteLine(stopWatch.Elapsed);

...打印00:00:00.0001274,这是时候了,我的很多的,因为我的应用程序在做很多其他的事情,有些是从老慢库,我一定要使用,不依赖于我。

... prints 00:00:00.0001274, which is alot of time for me, because my application is doing many other things, some of them from old slow libraries that I must to use and are not dependent on me.

这是如何实现更快的吗?任何想法

Any ideas on how to implement a faster one?

感谢你。

推荐答案

机会,你看到的JIT编译。在我的盒子,我看到:

Chances are you're seeing JIT compilation. On my box, I see:

00:00:00.0000360
00:00:00.0000060

当我快速连续在同一进程中运行了两次 - 而不是在调试器。 (确保你没有把它运行在调试器,或者这是一个毫无意义的测试。)

when I run it twice in quick succession within the same process - and not in the debugger. (Make sure you're not running it in the debugger, or it's a pointless test.)

现在,测量任意时间的的微小通常是一个坏主意。你需要重复数百次,以获得更好的主意的它采取了多久。

Now, measuring any time that tiny is generally a bad idea. You'd need to iterate millions of times to get a better idea of how long it's taking.

你有充分的理由相信它的实际上的放慢您的code - ?或者是你立足这一切在你原来的时机

Do you have good reason to believe it's actually slowing down your code - or are you basing it all on your original timing?

我怀疑,你会发现什么比显著快词典&LT; TKEY的,TValue&GT; 键,我会很惊讶地发现,它的瓶颈。

I doubt that you'll find anything significantly faster than Dictionary<TKey, TValue> and I'd be very surprised to find that it's the bottleneck.

编辑:我刚刚基准增加一个亿的元素到词典&LT; TKEY的,TValue&GT; ,所有的按键都存在的对象(字符串数组),再利用相同的值(因为它是不相关的),并指定一个容量一百万的建筑 - 它花了大约0.15秒在我二十岁的笔记本电脑

I've just benchmarked adding a million elements to a Dictionary<TKey, TValue> where all the keys were existing objects (strings in an array), reusing the same value (as it's irrelevant) and specifying a capacity of a million on construction - and it took about 0.15s on my two-year-old laptop.

确实的可能是你的瓶颈,因为你已经说你使用一些老慢库别的地方在你的应用程序?请记住,速度较慢的其他库,影响较小的改进的集合类都会有。如果字典更改仅约占整个应用程序的时间1%,那么,即使我们能提供的瞬间的字典,你只用1%加速您的应用程序。

Is that really likely to be a bottleneck for you, given that you've already said you're using some "old slow libraries" elsewhere in your app? Bear in mind that the slower those other libraries are, the less impact an improved collection class will have. If the dictionary changes are only accounting for 1% of your overall application time, then even if we could provide an instantaneous dictionary, you'd only speed up your app by 1%.

与以往一样,得到一个分析器 - 它会给你一个更好的主意,你的时间是怎么回事

As ever, get a profiler - it'll give you a much better idea of where your time is going.

这篇关于更快的更换,以字典&LT; TKEY的,TValue&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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