A faster replacement to the Dictionary

后端 未结 10 826
醉梦人生
醉梦人生 2020-12-08 15:03

I need a fast replacement for the System.Collections.Generic.Dictionary. My application should be really fast. So, the repl

10条回答
  •  醉梦人生
    2020-12-08 15:15

    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.

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

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

    EDIT: I've just benchmarked adding a million elements to a Dictionary 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.

    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.

提交回复
热议问题