Python & Music: Performance question
Nicodemus
nicodemus at globalite.com.br
Sun Apr 6 09:27:30 EDT 2003
David Asorey Álvarez wrote:
>Hello, group.
>
>I'm developing a small music program, and I have some doubts.
>
> I want define a class, Note, which represents a note. It has two
>attributes, pitch and duration (quarter note, half note, etc). There
>will be a lot of instances of this object, maybe hundreds of notes,
>and the program needs access to all of this objects and read its
>attributes.
> Which implementation will be more efficient?
>
>Implementation A:
>
>class Note:
> def __init__(self, pitch, duration):
> self.pitch = pitch
> self.duration = duration
> def change_pitch(factor): pass
> def change_duration(factor): pass
> # more methods ...
>
>
Try using __slots__... this will save memory.
class Note(object):
__slots__ = ['pitch', 'duration' ]
def __init__(self, pitch, duration):
self.pitch = pitch
self.duration = duration
# more methods
>Implementation B:
>
>class Note:
> def __init__(self, pitch, duration):
> self["pitch"] = pitch
> self["duration"] = duration
> def change_pitch(factor): pass
> def change_duration(factor): pass
> # more methods ...
>
>
This won't even work, unless you define a __setitem__ method. And if
you're looking for efficiency, that's not the way to go.
Hope that helps,
Nicodemus.
More information about the Python-list
mailing list