From 38026a43c57d8cc84df164244170a6232e7271c4 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sun, 14 Nov 2010 19:32:47 -0500 Subject: [PATCH] Optimize rand_candidate The set of searchable songs shouldn't change while selecting songs. We can determine which list of songs to look at up front and always use that as an argument to rand_candidate() --- libsaria/sources/playlist.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libsaria/sources/playlist.py b/libsaria/sources/playlist.py index e8668078..865b4cda 100644 --- a/libsaria/sources/playlist.py +++ b/libsaria/sources/playlist.py @@ -77,19 +77,23 @@ def seq_next(): cur_index += 1 return song_list[cur_index] -def rand_candidate(max): +def rand_candidate(list, max): index = rand.randint(0, max-1) - if filtered == False: - return song_list[index] - return list(visible)[index] + return list[index] def rand_next(): n = num_visible() if n == 0: return get_attrs = library.get_attrs + + if filtered == False: + selection = song_list + else: + selection = list(visible) + for i in xrange(15): - id = rand_candidate(n) + id = rand_candidate(selection, n) (artist, title, score) = get_attrs(id, "artist", "title", "score") if (artist, title) in recent: print recent_msg % (artist, title)