What is this cProfile result telling me I need to fix?

前端 未结 3 1965
刺人心
刺人心 2021-02-01 07:59

I would like to improve the performance of a Python script and have been using cProfile to generate a performance report:

python -m cProfile -o chrX         


        
3条回答
  •  我在风中等你
    2021-02-01 08:31

    This output is going to be more useful if your code is more modular as Lie Ryan has stated. However, a couple of things you can pick up from the output and just looking at the source code:

    You're doing a lot of comparisons that aren't actually necessary in Python. For example, instead of:

    if len(entryText) > 0:

    You can just write:

    if entryText:

    An empty list evaluates to False in Python. Same is true for an empty string, which you also test for in your code, and changing it would also make the code a bit shorter and more readable, so instead of this:

       for line in metadataLines:      
            if line == '':
                break
            else:
                metadataList.append(line)
    

    You can just do:

    for line in metadataLines:
        if line:
           metadataList.append(line)
    

    There are several other issues with this code in terms of both organization and performance. You assign variables multiple times to the same thing instead of just creating an object instance once and doing all accesses on the object, for example. Doing this would reduce the number of assignments, and also the number of global variables. I don't want to sound overly critical, but this code doesn't appear to be written with performance in mind.

提交回复
热议问题