Does None reserve any space in memory?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patelss23
    New Member
    • Jul 2007
    • 16

    #1

    Does None reserve any space in memory?

    Hello All,

    I have a list containing around a million integers.
    And I have to search this list for several million times. This really slow downs my
    program. I was thinking to convert this list to a dictionary.

    I will keep all integers as a key and None as their values.

    But I think it is going to need double amount of memory than list.
    So what I really don't know is that if None requires some kind of storage space in memory or not?

    Thank you all in advance,
    Sanket
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    What would converting your list into a dictionary achieve for you?

    I don't know for sure but I am willing to assume that a dictionary even if the keys all point to None pointers would consume more memory than a list.

    Comment

    • patelss23
      New Member
      • Jul 2007
      • 16

      #3
      Originally posted by jlm699
      What would converting your list into a dictionary achieve for you?

      I don't know for sure but I am willing to assume that a dictionary even if the keys all point to None pointers would consume more memory than a list.

      Hello jlm699,
      Thanks for your reply.

      Using dictionary for search would be faster thats why I want to use dictionary instead of list. I only want to know if the integer is in List( or in map) or not?

      Thanks,
      Sanket

      Comment

      • elcron
        New Member
        • Sep 2007
        • 43

        #4
        Try using a set, they're unordered an I believe faster than a list it also supports unions, intersections and differences which may or may not help

        Code:
        >>> s = set(i for i in xrange(101))
        >>> s
        set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100])
        >>> 10 in s
        True
        >>> s2 = s.intersection(set(i for i in xrange(50, 201, 2)))
        >>> s2
        set([50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])

        Comment

        • patelss23
          New Member
          • Jul 2007
          • 16

          #5
          Originally posted by elcron
          Try using a set, they're unordered an I believe faster than a list it also supports unions, intersections and differences which may or may not help

          Code:
          >>> s = set(i for i in xrange(101))
          >>> s
          set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100])
          >>> 10 in s
          True
          >>> s2 = s.intersection(set(i for i in xrange(50, 201, 2)))
          >>> s2
          set([50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])


          Thanks elcron,
          I will try that.

          Comment

          Working...