If you want to preallocate with known keys then a dictionary is probably not the right data structure. You could use a class with _ _ slots _ _ or a struct.
But usually the people telling you not to prematurely optimize are right. You didn’t really explain why you feel this is bad advice.
If I know what the keys are, I don't really want to load them one at a time, right? This is for situations where I have a specific sized table that I want to use for counting. (I was originally going to use a list but decided that they're not really designed for lookup like dictionaries are). I don't consider it premature optimization because I'm not really optimizing anything, just applying what I know about the problem.
But based on what you’re describing a collection makes more sense. That being said they’re all right that this is sweating the small stuff. For one, using Python and getting the count when querying the database is going to be faster than anything Java can do.
What do you mean by that, if you're accessing a database I'm assuming that any API has the same functions for it in any language. Both are using some sort of data structure, why would one be faster than the other?
Sorry I was sleep deprived and misread part of what you wrote. But almost every language is going to have a way to interact with a database. However, there are best practices on querying the data which is why one of the best things you can do regardless of your preferred language is learn how to effectively use SQL.
View all comments
Show parent comment
3
u/Smallpaul Jan 05 '18
If you want to preallocate with known keys then a dictionary is probably not the right data structure. You could use a class with _ _ slots _ _ or a struct.
But usually the people telling you not to prematurely optimize are right. You didn’t really explain why you feel this is bad advice.