簡體   English   中英

我怎樣才能讓我的函數運行得更快?

[英]How can i make my function run even more faster?

嘿伙計們 我正在解決問題 我對我的第一個功能做了很多更改以達到時間限制

但這真的是我最后的想法我不知道如何讓它比現在更快

from timeit import default_timer as timer
from datetime import timedelta

start = timer()

testList = ['hello']
wordList = ['hello','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe']
def words_with_given_shape(words,shape):
    itemList = 'abcdefghijklmnopqrstuvwxyz'
    def wordShape(word):
        tempS = []
        termil = len(word)-1
        
        for inx,elem in enumerate(word):
            orderAl = itemList.index(elem)
            if inx < termil:
                orderNl = itemList.find(word[inx+1])
                if orderAl > orderNl:
                    tempS.append(-1)
                if orderAl < orderNl:
                    tempS.append(1)
                if orderNl == orderAl:
                    tempS.append(0) 
        return tempS
    def checkWord(words):
        res = []
        for i in words:
            if wordShape(i)==shape:
                res.append(i)
        return res
    return checkWord(words)
print(words_with_given_shape(wordList,  [-1, 1, 0, 1,1,1,-1]))
print(words_with_given_shape(wordList,  [-1, 1, 0, 1]))
print(words_with_given_shape(wordList,  [-1, 1]))
print(words_with_given_shape(wordList,  [-1, 1, 0, 1,1, 0, 1,-1,1]))
print(words_with_given_shape(testList,  [-1, 1, 0, 1]))
end = timer()
print(timedelta(seconds=end-start))

它目前給我這個時間 0:00:00.001272

但似乎測試人員需要比這更快,因為在測試 12 時由於執行時間限制而失敗

那么基本上你能指導我使 words_with_given_shape 函數更加優化嗎?

*** 編輯 ***:我忘記告訴問題是它給出了單詞列表和單詞的形狀,形狀類似於 [0,1,1,-1] 這意味着 0 eq 1 個字符在當前字符之后字母順序 -1 字符在當前字符之前按字母順序排列

所以對於 Hello 它的 [-1, 1, 0, 1]

答案是找到單詞列表中形狀與形狀 arg 相同的所有單詞

嘗試這個:

def words_with_given_shape(words, shape):
    return [word for word in words
            if (len(shape) == len(word) - 1 and
                all(c1 == c2 if s == 0 else (c1 > c2 if s == -1 else c1 < c2)
                    for c1, c2, s in zip(word[:-1], word[1:], shape)))]

它更緊湊(只有一行)並且您不需要生成每個單詞的形狀(在這種情況下這是無用的,因為您可以使用提供的一個)!

通過像所示那樣重新編寫代碼並使用 List Comprehensions,您在 10000 次迭代中速度提高了 100 毫秒:

  • 在里面:
from timeit import default_timer as timer
from datetime import timedelta
start = timer()
testList = ['hello']
wordList = ['hello','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe']
  • v1:
def checkcond(x, i, word, itemList):
    _, __ = itemList.index(x),  itemList.find(word[i+1])
    return -1 if _ > __ else 1 if _ < __ else 0

def wordShape(word, itemList):
    return [checkcond(x, i, word, itemList) for i, x in enumerate(word[:-1])]

def words_with_given_shape(words,shape):
    itemList = 'abcdefghijklmnopqrstuvwxyz'
    return [x for x in words if wordShape(x, itemList)==shape]
  • v2:(在 google colab 上更快)
def words_with_given_shape(words,shape):
    itemList = 'abcdefghijklmnopqrstuvwxyz'
    def checkcond(x, i, word):
        _, __ = itemList.index(x),  itemList.find(word[i+1])
        return -1 if _ > __ else 1 if _ < __ else 0
    def wordShape(word):
        return [checkcond(x, i, word) for i, x in enumerate(word[:-1])]
    return [x for x in words if wordShape(x)==shape]
  • 時間檢查:
def timecheck():
    for x in range(10000):
        words_with_given_shape(wordList,  [-1, 1, 0, 1,1,1,-1])
        words_with_given_shape(wordList,  [-1, 1, 0, 1])
        words_with_given_shape(wordList,  [-1, 1])
        words_with_given_shape(wordList,  [-1, 1, 0, 1,1, 0, 1,-1,1])
        words_with_given_shape(testList,  [-1, 1, 0, 1])
    return timedelta(seconds=timer()-start)
print(timecheck())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM