How can I optimize this Python code to generate all words with word-distance 1?

前端 未结 12 843
予麋鹿
予麋鹿 2021-01-30 22:11

Profiling shows this is the slowest segment of my code for a little word game I wrote:

def distance(word1, word2):
    difference = 0
    for i in range(len(word         


        
12条回答
  •  粉色の甜心
    2021-01-30 22:21

    People are mainly going about this by trying to write a quicker function, but there might be another way..

    "distance" is called over 5 million times

    Why is this? Perhaps a better way to optimise is to try and reduce the number of calls to distance, rather than shaving milliseconds of distance's execution time. It's impossible to tell without seeing the full script, but optimising a specific function is generally unnecessary.

    If that is impossible, perhaps you could write it as a C module?

提交回复
热议问题