4

Probably this question has been answered many times before, in many different ways. I was looking for something more specific, however. Generic Dictionaries, I believe are faster than Hashtables, because they don't need to go through the process of boxing/unboxing.

However, aren't hashtables sorted, which means that searching could be faster? Since the keys are hashed and stored, will there be boxing/unboxing involved whilst searching?

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • It would only be boxing / unboxing if you are using value types (struct) in `Hashtable`. Otherwise its just casting when you want to get the specific type when retrieving the entry. Also you should use `HashSet` which is a generic lookup instead of `Hashtable`. – Igor Feb 27 '17 at 16:48
  • [Obligatory link when someone asks "which is faster?"](https://ericlippert.com/2012/12/17/performance-rant/) – DavidG Feb 27 '17 at 16:48
  • What makes you think that a hash table is sorting anything? – Servy Feb 27 '17 at 16:53
  • Possible duplicate of [.NET HashTable Vs Dictionary - Can the Dictionary be as fast?](https://stackoverflow.com/questions/1089132/net-hashtable-vs-dictionary-can-the-dictionary-be-as-fast) – Red Wei Oct 22 '18 at 09:12

2 Answers2

8

However, isn't hashtable's sorted, which could mean that the search could be faster?

I don't believe that hashtable is sorted.

They share a similar underlying implementation, but Dictionary<TKey, TValue> has been recommended over Hashtable for a long time, which will perform better for value types as it eliminates boxing/unboxing.

See https://referencesource.microsoft.com/#mscorlib/system/collections/hashtable.cs,77

If you really want to know, try benchmarking it. BenchmarkDotNet is a great library for that.

Christopher Currens
  • 29,917
  • 5
  • 57
  • 77
3

Hashtable is to be considered deprecated with the exception of backcompatibility and a few edge cases such as COM Interop.

Dictionaries also provide type safety and avoid the boxing process (which makes them quicker in general).

However, if you really want to know what performance impact it has, time both types performing an identical operation across two identical datasets using Stopwatch and note the time difference!

The code would look something like:

        Stopwatch clock = new Stopwatch();
        clock.Start();
        foreach (var item in myCol)
        {
            Hashtable ht = new Hashtable();
            //DoSomething()
        }
        clock.Stop();
        var tHash = clock.Elapsed;
        Stopwatch clock = new Stopwatch();
        clock.Start();
        foreach (var item in myCol)
        {
            Dictionary<,> dict = new Dictionary<,>();
            //DoSomething()
        }
        clock.Stop();
        Console.Write($"Delta t = {Math.Abs((tHash - clock.Elapsed).TotalMilliseconds)}"ms);
ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81
  • Hashtable is to be considered obsolete? If that's true, why did they add it to .Net Core? – DavidG Feb 27 '17 at 16:52
  • Except in a few edge cases where it is useful, it should probably be considered as such: http://stackoverflow.com/a/1590101/4700841 – ScottishTapWater Feb 27 '17 at 16:56
  • There's a big difference between being obsolete and it only being appropriate to use in certain situations. – DavidG Feb 27 '17 at 16:57
  • @DavidG I did say it's only in my opinion, for all intents and purposes it has been superseded by `Dictionary`. – ScottishTapWater Feb 27 '17 at 17:02
  • 1
    Your opinion is fine, but to call it obsolete is just incorrect. – DavidG Feb 27 '17 at 17:03
  • I beg to differ, but I'll change it to Deprecated if that would make it clearer. – ScottishTapWater Feb 27 '17 at 17:05
  • Haha well deprecated pretty much means the same thing. If that class were deprecated or obsoleted, then MS would have marked it as such. – DavidG Feb 27 '17 at 17:06
  • As far as I am aware, deprecation is slightly different as it allows for edge cases and indicates discouragement rather than prohibition of use. I don't think we'll agree on this though, either way, my response answers the question. – ScottishTapWater Feb 27 '17 at 17:10
  • 2
    @DavidG Your argument doesn't follow. MS isn't going to mark it as obsolete as as to not break the legacy code that is using it. That doesn't change the fact that only legacy code should be using it, and that there's never a reason to use it if you have access to `Dictionary`. The class *is* obsolete. – Servy Feb 27 '17 at 17:32
  • Exactly my thoughts @Servy – ScottishTapWater Feb 27 '17 at 17:33
  • @Servy I'm certainly not saying that it should ever be used, just that it's not been officially obsoleted. So yes, it's effectively deprecated, but it's still supported. It's just a terminology thing. Probably something like `WebClient` vs `HttpClient`, I would never use the former, but it's not obsolete. – DavidG Feb 27 '17 at 17:34
  • @DavidG "I'm certainly not saying that it should ever be used" If it shouldn't ever be used, then it's obsolete. Something being obsolete doesn't mean it's not supported. MS supports all sorts of obsolete things. – Servy Feb 27 '17 at 17:36
  • @Servy They've also ported it to .Net Core, that would have been a perfect time to drop it, yet they didn't. – DavidG Feb 27 '17 at 17:38
  • @DavidG They wanted it to be there for people porting old code. They don't want everyone using .NET Core to have to re-write all of their .NET code from scratch. – Servy Feb 27 '17 at 17:40
  • Just to throw this out there, Windows Vista is clearly obsolete, but is still supported (unless that's been changed recently) – ScottishTapWater Feb 27 '17 at 17:43
  • current ms docs " Important We don't recommend that you use the Hashtable class for new development. Instead, we recommend that you use the generic Dictionary class. For more information, see Non-generic collections shouldn't be used on GitHub." – pm100 Jun 10 '22 at 23:00