I have this block of code, which works, but takes about 8 seconds to execute. I know that it is the second for loop, since there is a loop inside of a loop. However, I believe that I need both loops, because I need to cross-reference the tracks list.
Does anybody know of a way to make this function execute faster? I can't seem to see another way of writing it.
FYI : The csv file that I am using has 5570 lines, which is another reason for the function taking a "while".
Thanks in advance!
def load_library(filename) : library = open(filename, 'rb') reader = csv.reader(library, delimiter = '|') tracks = set([]) albums = set([]) albums1 = set([]) #albums1 is the set of albums which have already been added to the albums list. for row in reader : artist, track, album, genre, year = row track = Track(artist, track) track.set_album(album) tracks.add(track) library = open(filename, 'rb') reader = csv.reader(library, delimiter = '|') for row in reader : artist, track, album, genre, year = row a = Album(artist, album) for i in tracks : if str(i.album) == str(a.title) : a.add_track(i.title) if album not in albums1 : albums.add(a) albums1.add(album) return tracks, albums After using c.Profile :
cProfile.run('load_library()') 224565 function calls in 9.776 seconds
Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.002 0.002 9.776 9.776 <string>:1(<module>) 5570 0.001 0.000 0.001 0.000 musiclib.py:18(set_album) 11140 0.007 0.000 0.007 0.000 musiclib.py:23(__init__) 92784 0.028 0.000 0.037 0.000 musiclib.py:31(add_track) 5570 0.004 0.000 0.009 0.000 musiclib.py:6(__init__) 1 9.723 9.723 9.775 9.775 musiclib.py:71(load_library) 2 0.000 0.000 0.000 0.000 {_csv.reader} 16710 0.002 0.000 0.002 0.000 {method 'add' of 'set' objects} 92784 0.009 0.000 0.009 0.000 {method 'append' of 'list' objects} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 2 0.000 0.000 0.000 0.000 {open}
readline(line_num)then to use aforloop for going through each line.cProfileto see which parts are slow.readline(line_num)to read every line?