I have a the following requirement to develop and am wondering what the fastest collection I could use in .net would be.
"To search a static collection of strings to find all string that start with a given string, the string can be in order".
Would a hashtable be the best to use? This is being done in c# .net 4.0.
Thanks
It is premature to optimize collection types - you don't even have working code!
Use a convenient collection type (Dictionary<TKey,TValue>, List<Tuple<T1,T2>> or whatever) - once you have working code, if there is a performance problem:
Profile the code
Identify the hotspots
If the collection you have selected is a hotspot, consider changing it.
Profile after the change to see you have fixed the problem
To find strings with a given prefix quickly, try a trie.
Related
In my application we have multi-lingual language strings which are stored in custom tables, as the user can edit, delete, import new languages etc... via a UI
Currently, what I'm doing is at the beginning of each request is. I'm going off and getting all the language strings (From our database) for the currently selected language and sticking them in a dictionary.
I then have a Html Helper extension method which I use in the razor views (See below), which fishes in the dictionary I got at the beginning of the request to pull out the correct language based on the key supplied in the helper.
Html.LanguageString("MyLanguage.KeyHere")
Now this works fine. However, as the application is getting bigger. We are getting more and more language strings. It's not an issue right now, as its still very fast as there are only around 200 strings to get.
But this also means I'm getting all of them, even if a page has say one on it. I'd ideally like a way of processing the LanguageString("")'s before hand and doing a query to just get those that are needed at the beginning of the request? Or maybe my own linq based language that can be processed and product a more efficient call.
I'm looking for some advice on how to do this. As I'd like the application to be as efficient as possible. Any advice, help, tips are greatly received. Thanks.
I'd suggest caching language strings on the application basis rather than fetching them for every request. For example, this can be done by maintaining a static dictionary and invalidating the cache only when the user makes changes to these strings. This will make your application more responsive as well as save you from implementing (imho) rather more complex and not necessarily efficient technique of loading this data on-demand.
As a side note I'd add the following: it's usually a good practice to address these kinds of problems when they arise (rather than fixing something that is not broken) and focus on more important things. I totally agree that performance implications of a given solution must always be taken into consideration, I'm just saying that premature optimizations are not always a good idea.
Hi everyone I am not sure if what I want to do is even remotely possible but I am going to do my best to explain it and would really appreciate any suggestions / ideas,
Imagine I have :-
public class Attribute{
object Value;
**(X1)**IComparer<T> comparer
}
public class AttributeValues
{
List<Attribute> values;
SortedList<Attribute> Sort(){
this uses the comparer defined in **(X1)** to sort the values in the list
}
}
These classes allow the user to create user defined attributes and select a pre-defined sort algorithm to sort the values
In the simplistic case, I can do AttributesValues.Sort() using one of the comparers that I have created and compiled in source code which has been chosen by the user. This is fine when the comparison is known in advance such as a simple ascending alphabetical sort for example.
However, there are some circumstances where more logic is needed and it is not known in advance. For example, the string "4DFG5ET" might codify a date that needs to be sorted and there may be some other attribute with similar logic and so on.
If possible I don't want to keep writing the IComparer implementations and would love for it to be possible that somehow I could define the IComparer implementation in a text box or file at runtime and it would be somehow compiled in the program or persisted then available in the selction of comparisons that could be used to sort an attribute.
Does anyone have any suggestions on how to approach this?
Please ask if you would like me to clarify something as I know it is a slightly obscure question.
Many thanks in advance
Alex
Sure, there are several different technologies that support that. Reflection.Emit comes to mind, and since the .NET framework includese a compiler you should also be able to emit C# and compile that on the fly to an assembly that you then load. There are more options in the new dynamic languages space, I believe.
You can make use of CSharpCodeProvider (see documentation here) to compile the provided text into a DLL and load the resulting class using reflection.
Also, this SO question might be of good help for you
I think you can use MEF for that:
quote:
"The Managed Extensibility Framework (MEF) is a composition layer for .NET that improves the flexibility, maintainability and testability of large applications. MEF can be used for third-party plugin extensibility, or it can bring the benefits of a loosely-coupled plugin-like architecture to regular applications."
As I see it, you have two options. First, you can construct your own language which can parse simple user commands and then execute the code in JIT fashion.
The second option is to actually let your users write C# code and the compile it on the fly. (As previously mentioned, Reflection.Emit should take care of most simple needs.)
EDIT
A third option isn't exactly what you wanted, but you could setup a directory for comparer plugins. When your application starts, it can scan that directory and load the assemblies. Using Reflection, you can extract any classes that implement the IComparer interface, then provide them as a list to the user. (You could also use custom attributes to define friendly user names and descriptions, etc).
In this case, you would not need to recompile your entire application just to quickly add some new comparer. Also, if you have a superuser, they could use some free .NET code editor to quickly write up their own comparer and "install" them to the directory.
Folks,
Given a set of sorted values (perhaps in a List<T>, SortedList<T,K>, etc.) what's the best way to go about evaluating inequalities (greater-than, less-than, greater-than-or-equal-to, less-than-or-equal-to a given value)? Possible with any of the standard .net types? Or easily coded up? Any pointers are greatly appreciated.
EDIT - of course I'm trying to make this as fast as possible. needs to be highly performant
If you mean something like the good-old lower_bound/upper_bound functions on C++ map<>, AFAIK there's nothing built-in in C#.
On List<T> there's a BinarySearch method implemented, but it works on exact matching only.
Anyway, you can easily implement it by yourself, peraphs using the code in this question as an example:
Is there a Lower Bound function on a SortedList<K ,V>?
Consider the following
A list of approximately 10,000 folders
Of these folders, a list of rules determine if they qualify to go to the next stage
The rules are a text based comparison such that
if folder name contains (...any of the following from a list of exceptions) - such that there is a one to many comparison for each folder, but the folder name string must contain (or must NOT contain) any of the strings it is compared do
I'm relatively new to C# so I'm not entirely sure what's under the hood of each class
Any advice in some general direction would be greatly appreciated.
Do you have a performance problem, or are you trying to optimize the code before it has been written?
The Comparer class is typically not the topmost performant class of the .NET framework, but it has to cater for quite a lot of scenarios.
If you know the source and target types, you're usually better off implementing your own specific comparer class.
However, unless you know that you have a performance problem, I wouldn't worry too much about it.
First of 10 K folders is not a large number... So you might not want to worry about performance already.
So dont optimize...
After that you might want to consider the way you search your names... Instead of a seach for every single element, you could create a regexp that will perform all searches at once, but that is optimization without a real need...
First you need a reason to change the code
This may be a silly question (with MSDN and all), but maybe some of you will be able to help me sift through amazing amounts of information.
I need to know the specifics of the implementations of common data structures and algorithms in C#. That is, for example, I need to know, say, how Linked Lists are handled and represented, how they and their methods are defined.
Is there a good centralized source of documentation for this (with code), or should I just reconstruct it? Have you ever had to know the specifics of these things to decide what to use?
Regards, and thanks.
Scott Mitchell has a great 6-part article that covers many .NET data structures:
An Extensive Examination of Data Structures
For an algorithmic overview of data structures, I suggest reading the algorithm textbook: "Introduction to Algorithms" by Cormen, et al..
For details on each .NET data structure the MSDN page on that specific class is good.
When all of them fail to address issues, Reflector is always there. You can use it to dig through the actual source and see things for yourself.
If you really want to learn it, try making your own.
Googling for linked lists will give you a lot of hits and sample code to go off of. Wikipedia will also be a good resource.
Depends on the language. Most languages have the very basics now pre-built with them, but that doesn't mean their implementations are the same. The same named object--LinkedList in C# is completely different than the LinkedList in Java or C++. Even the String library is different. C# for instance is known to create a new String object every time you assign a string a new value...this becomes something you learn quickly when it brings your program to a crashing halt when you're working with substrings in C# for the first time.
So the answer to your question is massively complicated because I don't know quite what you're after. If you're just going to be teaching a class what a generic version of these algorithms and data structures are, you can present them without getting into the problems I mentioned above. You'll just need to select, lookup, read about a particular type of implementation of them. Like for LinkedList you need to be able to instantiate the list, destroy the list, copy the list, add to the list somewhere (usually front/back), remove from the list, etc. You could get fancy and add as many methods as you want.