Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm writing a service where performance is essential, and I'm not sure what is the fastest thing. I have a few Objects (50-200) which each have an ID in them (ints, e.g. 84397 or 23845). Would it be faster to have a Dictionary, a List of KeyValue Pairs or a List with the indexes set to the IDs with the rest having null values or an array with the same idea?
It depends on which operation you want to execute. Let's assume that you want to find an object with a given ID.
The huge array approach is fastest: Accessing myArray[84397] is a constant-time operation O(1). Of course, this approach requires the most memory.
The dictionary is almost as fast but requires less memory, since it uses a hash table internally.
The list of pairs approach is the slowest, since you might have to traverse the whole list to find your entry, which yields O(n) complexity.
Thus, in your situation, I would choose the dictionary, unless the marginally better performance of the huge array is really relevant in your case.
Dictionary<TKey, TValue> uses a hash table internally so I think it would be the fastest one.
Dictionary versus List Lookup time
Also, for a more detailed explaination of the different collections, check out this question.
You can use Hashtables as well. Dictionary internally using it anyway.
but dictionary has an advantage that it is a GENERIC type which gives you type safety.
here is different thread
Dictionary Vs HashTable
I hope it helps you decide.
Praveen
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a set of data that I should be able to filter by two keys, which are strings.
So I've been wondering what is the faster way to do it.
The most obvious way is a nested dictionary
Dictionary<string, Dictionary<string, Data>>
but I've been also thinking about either using a tuple
Dictionary<(string, string), Data>
or combining the strings to create a key
string.Format("{0}:{1}", key1, key2)
Dictionary<string, Data>
Now this option I doubt is faster, since every lookup is gonna be coupled with the formatting of the key
There's also the option of creating a struct, which will act as the key
dataKey(key1, key2)
Dictionary<dataKey, data>
Or maybe for such case something else than a dictionary would be a better fit?
In that case I'd add that in the actual application, I first search for key1+key2 combination, and if there's no matching data, I find it using only the key2 and use a placeholder key1.
Nested dictionary lookup is a 2 lookup operations.
If you combine your two keys into one, you will avoid the second lookup operation, at the cost of a new string allocation.
It should be more efficient to have a specific structure as key that contain the two keys, and to implement GetHashCode() and Equals()
But as said by Jeroen, you should write you own benchmarks with tools like Benchmarkdotnet for real figures.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
AsReadOnly() is a convenient method to get a read-only image (not expensive compared to immutable collections' copies) of a collection. I am wondering if ImmutableList has AsReadOnly like method? If no, any easy way to implement similarly?
Immutable collections are inherently read-only. You can easily check in the documentation that ImmutableList<T> already implements IReadOnlyList<T> and IReadOnlyCollection<T> interfaces.
Memory is not allocated when you access elements from immutable collection. On the other hand, when you add an element to some immutable data structure, a new immutable collection is created (and some memory is used). Many immutable collections' implementations do not copy all the data to a new collection but instead share some data from with the old one, so in most cases you should not be too concerned with memory usage/allocation time.
Some collections, e.g. ImmutableHashSet<T>, have a documentation which states that they are optimized in terms of number of memory allocations.
The idea behind sharing some data between immutable collection is not complicated. Wikipedia has an simple example (with a nice diagram) showing how memory can be saved in case of immutable singly-linked lists.
ImmutableList<T> is copied by reference so is perfectly safe to pass around without a performance penalty. Thus there is no need for an AsReadOnly method as it wouldn't make it any easier to copy.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to know what is more optimal, A number N of private integer variables, or a single array containing N integer values.
When you use an array, that is a plus indirection. The array will be allocated at a separate part of the memory, so when you first access it, your code first obtains its address, then it is able to read out its content. It also needs some indexing, but that is done extremly fast by the CPU. However, .NET is a safe environment and it will do a check whether you use a valid array index. It adds additional time.
When you use separate variables, these will be encompassed by your object instance and no indirection is needed. Also, no index bound check is needed.
Moreover, you cannot name nicely the Nth element of an array, but you can give good names for individual variables. So your code will be readable.
As others mentioned, you shouldn't do this kind of optimalizations, the compiler/jitter take care of it. The compiler knows several common use cases and has optimialization strategy for that. If you start doing tricky things, the compiler will not recognize your intention and cannot make the optimalization for you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
We can use any of these (includes List, ArrayList, Dictionary, Hashtable, Stack, Queue) to hold value or hold reference to other objects as a collection.
But, my question is which one is used when?
Lists
Lists allow duplicate items, can be accessed by index, and support linear traversal.
ArrayList - An array-based list that doesn't support generic types. It does not enforce type safety and should generally be avoided.
List - An array list that supports generic types and enforces type-safety. Since it is non-contiguous, it can grow in size without re-allocating memory for the entire list. This is the more commonly used list collection.
Hashes
Hashes are look-ups in which you give each item in a list a "key" which will be used to retrieve it later. Think of a hash like a table index where you can ask questions like "I'm going to find this object by this string value. Duplicate keys are not allowed.
HashTable - A basic key-value-pair map that functions like an indexed list.
Dictionary - A hashtable that supports generic types and enforces type-safety.
Queues
Queues control how items in a list are accessed. You typically push/pop records from a queue in a particular direction (from either the front or back). Not used for random access in the middle.
Stack - A LIFO (last in, first out) list where you push/pop records on top of each other.
Queue - A FIFO (first in, first out) list where you push records on top and pop them off the bottom.
List can hold duplicate objects
ArrayList is just for compatibility with older versions of the framework where IList didn't exist
Dictionary is used to store pairs of key/value. You cannot have duplicate keys.
Hashtable is basically a List with no possibility of duplicates (and better performance in some scenarios)
Stack stores objects in order they were added (through Push()), and when you retrieve an object (through Pop()) it is removed from the stack in a LIFO manner.
Queue quite similar to a Stack except it is FIFO.
Here are some uses for them.
List: If you just want a list and don't care about any duplicates, i.e list of people, shopping list, list of things to do in life.
Queues: If you want to simulate a queue for example, in a hospital you have a queue and also priority queue (in emergency departments). The triage would determine who is in critical condition and needs to be treated.
Another example is a shopping queue, first person in line is 'usually' the first one to checkout.
Stacks: Used in your internal memory to push and pop values as you pass them to functions/methods.
Another interesting use is, in video game inventory method, where you can pick up an item (push) onto the stack, and drop an item (pop) off the stack.
Hash/Dictionary: These are usually seen used in database, for look up and index.
Depending on what you want to simulate, I do agree with the others, it's handy to read up on data-structures. A book helps but the internet also has a wealth of information.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm learning c# , and I reached the LinkedList<T> type and I still need to know more, like when should I use it, how do I create one, how do I to use it. I just want information.
If any one knows a good article about this subject, or if you can show me some examples with explanation, such as how to create, how to add and remove, and how to deal with nodes and elements.
Thanks in advance. I really enjoy asking questions around here with all the pros answering and helping.
[EDIT] Changed reference to LinkedList<T> instead of "array linkedlist." I think this was what was meant based on the context.
You can find more information on LinkedList<T> at MSDN, including an example of how to create and use one. Wikipedia has a reasonable article on linked lists, including their history, usage, and some implementation details.
Linked is a collection to store sequences of values of the same type, which is a frequent task, e.g. to represent a queue of cars waiting at a stoplight.
There are many different collection types like linked list, array, map, set etc. Which one to use when depends on their properties, e.g.:
do you need some type of ordering?
is the container associative, storing key-value pairs like a dictionary?
can you store the same element twice?
performance measures - how fast is e.g. inserting, removing, finding an element? This is usually given in Big-O notation, telling you how the time required scales with the number of elements in the collection.
Memory footprint and layout. This also can affect performance, due to good/bad locality.
This collection class implements a doubly linked list. It allows you to quickly determine the immediate sibling for a specified item in the collection. Removing an item from the collection automatically resizes it so that it does not leave any gaps.
For more info on LinkedList class, check out LinkedList at MSDN.
Do you know what a standard Linked List is? It's like one of those (doubly linked) but using .NET Generics to allow you to easily store any Type inside of it.
Honestly, I don't use it, I prefer the more basic List or Dictionary.
For more info on Linked Lists, check out wikipedia. As for generics, there are tons of articles here and at MSDN.
linklist are collections.. they can be use as replacements for arrays.. they can dynamically grow in size and has special helper methods that can help the development or the problem solving be faster.. try to view its methods and properties to understand more.
linklist is a generic collection.. meaning can used to declare type safety declarations..