Not a Python guy--trying to implement this sort faster. Currently I have a hash that includes objects, and I'm sorting them on a call to a method on those objects. I'm not certain how sorted() is functioning--is this making multiple method calls per comparison? Would I be better off perhaps storing the method call on the hash itself and sorting on that?
sorted(hash_object.items(), key=lambda x:x[1].method_call_here())
Currently taking ~100-400ms which is a fairly slow sort. Thoughts?
Responding to what the method call is here; I'm skeptical that it's the method. It's a direct port of my Ruby implementation which runs at 0.2ms, but maybe it's slower in Python for some reason. Really simple method though. It's calling the track quality method below:
class Track:
def __init__(self, title, play_count, track_number):
self.title = title
self.play_count = play_count
self.track_number = track_number
def predicted_listens(self):
return 1/self.track_number
def track_quality(self):
return self.play_count/self.predicted_listens()
For reference, it would seem like it's implementing something identical to the Ruby source:
self.sort_by { |track| track.quality }
My guess is I'm wrong about what's happening under the hood.
1/self.track_numberwill be truncated ifself.track_numberis anintin Python2hash_object.items()tohash_object.iteritems().