Increasing speed of python code

前端 未结 8 1860
野性不改
野性不改 2021-02-05 12:37

I have some python code that has many classes. I used cProfile to find that the total time to run the program is 68 seconds. I found that the following function in

8条回答
  •  情歌与酒
    2021-02-05 13:08

    This comment rings alarm bells:

    '''Returns quantity demanded in period timePd. In addition,
    also updates the list of customers and non-customers.
    

    Aside from the fact that timePd is not used in the function, if you really want just to return the quantity, do just that in the function. Do the "in addition" stuff in a separate function.

    Then profile again and see which of these two functions you are spending most of your time in.

    I like to apply SRP to methods as well as classes: it makes them easier to test.

提交回复
热议问题