hyperPad hyperPad Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Login

    Is a dictionary or an array faster?

    Scheduled Pinned Locked Moved
    Help and Support
    2
    3
    134
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • iTap DevelopmentI
      iTap Development
      last edited by

      If I have position x and y and rotation and want to be setting objects to those values, would it faster to have them as keys in a dictionary, or as indexes in an array?

      D 1 Reply Last reply Reply Quote 0
      • D
        Deeeds @iTap Development
        last edited by Deeeds

        @iTap-Development when in doubt, use arrays.

        Arrays are faster for almost everything, by an order of magnitude. Particularly in Objective-C, which is how they'd be implemented.

        The process of looking up, via a key, is quite slow, when compared with index lookups of arrays.

        When it comes to iterating through one or the other, arrays are often much more than an order of magnitude faster due to their inherently streamlined memory usage.

        Then there's unpacking.

        When you store x, y and rotation in a dictionary at a key, you have to "unpack" that to use it, which takes even longer.

        So you're best off having an array for each:

        arrayX
        arrayY
        arrayRotation
        arrayObjectName

        and iterating through when you need.

        iTap DevelopmentI 1 Reply Last reply Reply Quote 0
        • iTap DevelopmentI
          iTap Development @Deeeds
          last edited by

          @Deeeds thanks!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post