Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DavidDebnar · Jun 26, 2013 at 07:36 PM · arraydictionarygenerics

Array of dictionaries or one massive dictionary?

Alright, I'm facing this decision. Should I make an array of dictionaries, where each element in an array would be a dictionary, and to access it I'd call

 dictionaryArray[index]["dictionary_key"] = "foo";

or instead have a massive dictionary and use the index as a prefix?

 massiveDictionary[index+"dictionary_key"] = "foo";

I'm asking mainly, because I don't know what the algorithms of generic dictionaries are, and whether the increasing number of elements impacts their performance or not.

--David--

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BlueRaja_2014 · Jun 26, 2013 at 08:25 PM 0
Share

The first one will be faster, due to the second having a slow (in comparison) string-concatenation. But, there is likely a better overall solution to your problem. What is the actual problem you're trying to solve?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by DocteurCox · Jun 26, 2013 at 08:28 PM

The Dictionary generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the Dictionary class is implemented as a hash table. Note The speed of retrieval depends on the quality of the hashing algorithm of the type specified for TKey.

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

I think performances should not be a major issue :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DavidDebnar · Jun 26, 2013 at 08:41 PM 0
Share

Wow, I didn't know dictionaries were O(1) access time. Anyways, thanks for the pointer :) I'll test this out and then comment with my final solution.

avatar image BlueRaja_2014 · Jun 26, 2013 at 08:54 PM 1
Share

Technically, dictionary access is not O(1), it's amortized ("average") O(1). Array-access is for real-real O(1). In either case, as I mentioned above, the bottleneck for this piece of code is going to be the string-concatenation, which will likely be slower than either of those access-patterns.

However, I agree with @DocteurCox that this is highly unlikely to be the bottleneck in your program, unless you are calling it on the order of 100,000+ times per frame.

So, it probably won't make a difference.

avatar image DavidDebnar · Jun 26, 2013 at 08:57 PM 0
Share

Ok, again, I thank you both for your inputs on this ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Load txt file (CSV) into Generic Dictionary Javascript 1 Answer

Storing level data from a texture file, and drawing at speed. 0 Answers

Can Vexe.FastSave save dictionaries to files? 1 Answer

Is it possible to change the element name in an Array List? 1 Answer

Array member treated as null even though it is not. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges