C
C#2y ago
ero

Fast look-up algorithms

I'm looking to use a sort of cache of objects which are stored each with a unique key. Currently, I do this simply using a Dictionary<TKey, TValue>. This is to prevent having to fetch the items anew, when I can simply get them from that cache. My question is, what are some quicker and more efficient collections or algorithms to use for storing pairs of keys and values and fetching them when needed? (Obviously the logic to "refresh" the cache will run when an item cannot be found.)
12 Replies
jcotton42
jcotton422y ago
You're not gonna get faster than an in-memory dictionary
ero
ero2y ago
Oh? I don't know much about this stuff, but i see O(n) and O(log n) thrown around a lot. Isn't Dictionary O(n) and therefore on the worse side? Or was this strictly about sorting and I'm conflating it with lookups
jcotton42
jcotton422y ago
Typically it's more efficient than that As in, dictionaries and hashtables in general
ero
ero2y ago
Hm, does that include TryGetValue? Most of my TKeys will be strings
jcotton42
jcotton422y ago
I don't see why it wouldn't
reacher
reacher2y ago
O(1) lookups for the typical case is the whole reason for dictionaries So yes, that's what you want
ero
ero2y ago
actually, i guess the speed would depend on how quick the TKey's Equals method is, right?
reacher
reacher2y ago
What makes you say that?
ero
ero2y ago
i mean, do the keys not still need to be compared for equality in some way?
reacher
reacher2y ago
So there's a couple of problems with that statement Keys are put in buckets by their hash code You normally don't want to have hash code collisions causing multiple keys to fall in the same bucket If they do happen to fall in the same bucket, you do have to compare the keys by value However, comparing the keys by value won't affect the big O speed
jcotton42
jcotton422y ago
Big-O is about how the time grows with size O(1) doesn't mean "fast" It means "the time won't increase as the collection size increases"
Want results from more Discord servers?
Add your server
More Posts
Why is react .net core not serving my SPA files?? [Answered]I have been trying to do a very simple thing for a very long time and could really use some help. I XAML Objects?In the screenshot does creating something like this label mean that it's creating a nameless object?Could not load file or assembly 'System.Collections.Imutable' when using source generatorsI am trying source generators, and now i am getting warning ```Generator failed to generate source. Generating random numbers (int32) [Answered]As you may know ``Guid.NewGuid()`` returns a 128bit random "number" which is very unlikely to get duVisual Studio doesn't work as expected with source generators [Answered]I wanted to give source generators a try, i followed Microsoft tutorial and everything worked as expBlazor WASM Hosted uses wrong basepath on page reloadHello! I have a web app that mostly works as it should, except on a Page Refresh or direct accessingCalling a static abstract method without a generic type [Answered]If I have this code with a static abstract member `Bar` in the interface `IFoo`, is there *any way* WinUI3 app doesn't run outside of Visual StudioI just tried to share a tool I made in WinUI3 with .NET6. That shouldn't be a problem with the publiLearning Agile and Scrum on my own, looking for advice, resources, insightsI haven't had the chance to work both in groups of developers nor in groups of developers working unProper C multicatch syntaxI saw in Java that you can do multicatch blocks (see image) I also looked it up on stackoverflow: