Python for loop效率

时间:2014-10-05 17:42:48

标签: python performance loops csv for-loop

我有这个代码块,它可以工作,但需要大约8秒才能执行。我知道它是第二个for循环,因为循环内部有一个循环。但是,我认为我需要两个循环,因为我需要交叉引用tracks列表。

有没有人知道如何让这个功能更快地执行?我似乎无法看到另一种写作方式。

仅供参考:我使用的csv文件有5570行,这是该函数采用"而#34;的另一个原因。

提前致谢!

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

使用c.Profile后:

  
    
      

cProfile.run(' load_library()')                224565次函数调用9.776秒

    
  
   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}

1 个答案:

答案 0 :(得分:5)

只在一个for循环中执行此操作:

def load_library(filename) :
    library = open(filename, 'rb')
    reader = csv.reader(library, delimiter = '|')
    tracks = set([])
    albums = {}

    for row in reader : 
        artist, track, album, genre, year = row
        if album not in albums:
            a = Album(artist, album)
            albums[album] = a
        else:
            a = albums[album]
        a.add_track(track)
        track = Track(artist, track)
        track.set_album(album)
        tracks.add(track)
    return tracks, set(albums.values())