両方のコードが同じ目的です: - 豚ラテン語は、単語の最初の文字を最後に移動し、 "ay"を追加する言語ゲームです。だから "Python"は "ythonpay"になります。どのコードが速度と処理の面で優れていますか?
pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
word= original.lower()
first= word[0]
new_word= word +first + pyg
new_word= new_word[1:len(new_word)]
print new_word
else:
print 'empty'
またはtimeit
ライブラリからビット助けを借りて、この
user = raw_input("what is the word you want to play with:").lower()
if user.isalpha() and len(user) > 0:
print user[1:] + user[0:1] + "ay"
else:
print "please enter valid name!"
それぞれを10000回実行し、どれくらいの時間がかかるかを比較します。 –
関数1:0.572109937668関数2:0.469521999359コードのperformaeのこの小さなdiffrence問題です –