I had the following method in my code. My boss asked "why are you using a dictionary", you can just use an array, it's more efficient:
public static Dictionary<string, string> GetLis开发者_StackOverflow中文版tOfMonths()
{
Dictionary<string, string> months = new Dictionary<string, string>();
months.Add("1", "Jan");
months.Add("2", "Feb");
months.Add("3", "Mar");
months.Add("4", "Apr");
months.Add("5", "May");
months.Add("6", "Jun");
months.Add("7", "Jul");
months.Add("8", "Aug");
months.Add("9", "Sep");
months.Add("10", "Oct");
months.Add("11", "Nov");
months.Add("12", "Dec");
return months;
}
I use this for code reuse..so that I can bind to some month dropdown menus in various parts of our application.
An array is certainly more efficient for reasonably dense collections based on an integer index.
On the other hand, an array won't let you use a string
key as you've got for your dictionary.
Back to the first hand, why do you want a string key for your dictionary?
I wouldn't get too hung up on the performance aspect though - write the most readable code. If you're sure that using a string key is more appropriate here, then go ahead with the dictionary. The alternative (if you've been presented with a string) is to parse the integer and index into the array.
It's not really feasible for us to say which will be the best solution here.
You should say to your boss: "I use a dictionary because it leads to more readable and maintainable code. If during testing we detect performance problems in the application, and profiling demonstrates that it is the fault of the dictionary, then I will try with an array."
The basic cycle of performance testing is as follows:
- Define performance requirements
- Measure actual performance
- If actual performance is >= requirements, stop
- Profile application and find bottleneck(s)
- Try to fix bottlenecks
- Go to 2
By "define performance requirements" I mean you need concrete things that can be quantitatively measured, for example:
We require that the application can handle at least 100 concurrent users, with think times centred around the mean using normal distribution, each having a time to last byte (TTLB) of no more than 2 seconds, carrying out the following set of key business scenarios: 50% browsing products, 10% purchasing products, 20% doing some other thing, 20% doing yet another thing.
If you haven't done (1) then you cannot proceed to (2) so hang on until you've done that. If you've done (1) then you can follow the remainder of the steps, and the answer as to whether you need to optimise this code will drop out of that.
Well.. if you need to put something with fixed, continuous numeral indexes - indeed, there is no point adding 1,2,3,4,5...etc because simple array will do it for you.
Your boss is right, you should change it to string[] array or StringCollection.
As far as I know, a Dictionary is only slightly less efficient than a List, if you're creating a business application, then it probably won't be the performance bottleneck.
However, semantically, a List would probably be just as good, unless you want to explicitly define the month indices (e.g. for clarity).
P.S. If you use a Dictionary, I'd rename the method to GetDictionaryOfMonths() GetMonths()
Well, i just finished a benchMark to discover which is more fast way (List,array, dictionary) and make a 1000 times loop, and finally discover than dictionary is slightly (2%) more faster.
This sounds like an argument of trading memory for clock cycles. It's true that the Dictionary will have more extra overhead but if it's accessed frequently you should actually save clock cycles in the long run because you won't have to be looping through an array every time you want to look up a value.
But the main consideration here can be readability. Dictionary looksups are much easier to follow in code that array lookups. A little bit of overhead, very little with such a small collection, is well worth it IMHO.
Also for a bit of refactoring i would use Dictionary<int, string>
for more obvious type safety and utilization. Doesn't make sense to use a string where an int is called for.
Your boss is correct. Since you are going to access it by the month number, you can have O(1) access to the value without the additional overhead of setting up the dictionary.
So I just read that you are populating a drop down list. Instead of doing a look up to get the abbreviated name of the month, just put that as the drop down list value.
Your boss is right but a switch is faster yet, so if you really want performance (only applicable if the above code is actually a bottleneck) use a switch instead. It Will be harder to read but it Will be faster
精彩评论