What is more efficient?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Karlo Lozovina

    #1

    What is more efficient?

    Let's say I have a class with few string properties and few integers, and
    a lot of methods defined for that class.

    Now if I have hundreds of thousands (or even more) of instances of that
    class - is it more efficient to remove those methods and make them
    separate functions, or it doesn't matter?

    Thanks...

    --
    _______ Karlo Lozovina - Mosor
    | | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
    | || _ | _ | Parce mihi domine quia Dalmata sum.
    |__|_|__||_____ |_____|
  • placid

    #2
    Re: What is more efficient?

    On Feb 19, 2:17 pm, Karlo Lozovina <_karlo_@_mosor .net_wrote:
    Let's say I have a class with few string properties and few integers, and
    a lot of methods defined for that class.
    >
    Now if I have hundreds of thousands (or even more) of instances of that
    class - is it more efficient to remove those methods and make them
    separate functions, or it doesn't matter?
    What do you mean by efficient. Memory efficient? I would assume that
    making them separate functions would use less memory but would you
    sacrifice code readability for that (small?) of a memory saving?

    Good coding practice suggests that the methods should be bound to
    classes.
    >
    Thanks...
    >
    --
    _______ Karlo Lozovina - Mosor
    | | |.-----.-----. web:http://www.mosor.net|| ICQ#: 10667163
    | || _ | _ | Parce mihi domine quia Dalmata sum.
    |__|_|__||_____ |_____|


    Cheers


    Comment

    • Gabriel Genellina

      #3
      Re: What is more efficient?

      En Mon, 19 Feb 2007 00:17:54 -0300, Karlo Lozovina <_karlo_@_mosor .net_>
      escribió:
      Let's say I have a class with few string properties and few integers, and
      a lot of methods defined for that class.
      >
      Now if I have hundreds of thousands (or even more) of instances of that
      class - is it more efficient to remove those methods and make them
      separate functions, or it doesn't matter?
      I'm not sure what you mean, but normal methods are attached to the class,
      not to its instances. It doesn't matter whether you have 0 or a million
      instances, methods do not occupy more memory.

      --
      Gabriel Genellina

      Comment

      • Gary Herron

        #4
        Re: What is more efficient?

        Karlo Lozovina wrote:
        Let's say I have a class with few string properties and few integers, and
        a lot of methods defined for that class.
        >
        Now if I have hundreds of thousands (or even more) of instances of that
        class - is it more efficient to remove those methods and make them
        separate functions, or it doesn't matter?
        >
        Thanks...
        >
        It is not noticeably more or less efficient in either memory or
        execution speed.

        Both method and function code is compiled and stored once in either the
        class name space (for the method) or the module name space (for the
        function), and the lookup of either one is a single lookup in the
        appropriate name space.

        Actually the method lookup requires one extra step: First look for the
        method in the instance (which fails) and then look for it in the class
        (which succeeds). But that extra look up is highly optimized and
        probably not noticeable. The number of methods/functions may slow things
        up, but it will affect either name space equally.

        Gary Herron


        Comment

        • placid

          #5
          Re: What is more efficient?

          On Feb 19, 4:38 pm, Gary Herron <gher...@digipe n.eduwrote:
          Karlo Lozovina wrote:
          Let's say I have a class with few string properties and few integers, and
          a lot of methods defined for that class.
          >
          Now if I have hundreds of thousands (or even more) of instances of that
          class - is it more efficient to remove those methods and make them
          separate functions, or it doesn't matter?
          >
          Thanks...
          >
          It is not noticeably more or less efficient in either memory or
          execution speed.
          well lucky i wont be designing/creating a new language!
          >
          Both method and function code is compiled and stored once in either the
          class name space (for the method) or the module name space (for the
          function), and the lookup of either one is a single lookup in the
          appropriate name space.
          >
          Actually the method lookup requires one extra step: First look for the
          method in the instance (which fails) and then look for it in the class
          (which succeeds). But that extra look up is highly optimized and
          probably not noticeable. The number of methods/functions may slow things
          up, but it will affect either name space equally.
          >
          Gary Herron

          Comment

          • Scott David Daniels

            #6
            Re: What is more efficient?

            Karlo Lozovina wrote:
            Let's say I have a class with few string properties and few integers, and
            a lot of methods defined for that class.
            >
            Now if I have hundreds of thousands (or even more) of instances of that
            class - is it more efficient to remove those methods and make them
            separate functions, or it doesn't matter?
            >
            Thanks...
            >
            You'd do best to define the instance variable names in __slots__,
            and be sure to inherit from object. The methods don't matter (they
            all hang off the class anyway). The __slots__ will save you one
            reference per object for the __dict__.

            --
            --Scott David Daniels
            scott.daniels@a cm.org

            Comment

            • Karlo Lozovina

              #7
              Re: What is more efficient?

              "Gabriel Genellina" <gagsl-py@yahoo.com.ar wrote in
              news:mailman.41 84.1171862407.3 2031.python-list@python.org :
              It doesn't matter whether you have 0 or a million instances,
              methods do not occupy more memory.
              That's what I was looking for!
              Thanks, to you and all the others.

              --
              _______ Karlo Lozovina - Mosor
              | | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
              | || _ | _ | Parce mihi domine quia Dalmata sum.
              |__|_|__||_____ |_____|

              Comment

              Working...