what’s the difference between [NSMutableDictionary dictionary] and [NSMutableDictionary dictionaryWithCapacity:10]?
which one is better?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
dictionaryWithCapacityis better if you know how many elements you are going to put in at the start. This means it can immediately allocate the required amount of space for your items.It does not prevent you from adding more. It just means that if you exceed the allocated amount, it will have to allocate more, which may be more resource intensive.
On the flipside, if you don’t know how many items you need, and you allocate too much space, you’re not being memory efficient which is very important in mobile devices.
I don’t have any benchmarks, but I don’t believe the different is terribly huge, so I wouldn’t be too fussed.