I’m using a Python object with a couple attributes to organize a data model, but I’m wondering if this is any less efficient than using a key based dictionary. My model stores MP3 tag data and looks as such:
class Mp3Model:
def __init__(self, path, filename):
self.path = path
self.filename = filename
self.artist = ''
self.title = ''
self.album = ''
self.tracknumber = ''
self.genre = ''
self.date = ''
The model is used as such:
mp3s = []
for file in files:
if os.path.splitext(file)[1] == '.mp3':
# Append a new Mp3Model to the mp3s list for each file found
mp3s.append(Mp3Model(os.path.join(self.dir, file), file))
Would using a key based dictionary, or even a simple list provide much performance enhancement? The length of the mps[] object list is highly variable depending on how many files are found in a given directory, and the program can slow to a crawl (I haven’t implemented any threading yet) when I scan directories with tons of files.
Unless you declare
__slots__for your object, object attributes are stored in an underlyingdictanyway so using adictwould be marginally faster than an object. However the difference would be negligible compare to the rest of your code.The selection of a data structure should depend on various other factors:
Optimising for your use case would lead to much higher returns.