简体   繁体   English

编写相同代码的最有效方法

[英]Most efficient way to write the same code

I've just started learning python, and since there are different ways to write the same thing, I was wondering if some ways were more efficient than other.我刚开始学习 python,由于有不同的方法来编写相同的东西,我想知道是否有些方法比其他方法更有效。 For example, I've written two versions of this super basic code that will print the longest of 3 words;例如,我编写了这个超级基本代码的两个版本,将打印 3 个单词中最长的一个; is one of the two versions more efficient than the other?两个版本中的一个比另一个更有效吗? Thank you.谢谢你。

#print the longest word 1 #打印最长的单词1

def long_word (word1,word2,word3):


    if word1.__len__() > word2.__len__() and word1.__len__()>word3.__len__():
        print ("the longest word is" + word1)
    elif word2.__len__() >word1.__len__() and word1.__len__()()>word3.__len__():
        print ("the longest word is" +word2)
    elif word3.__len__() >word1.__len__() and word3.__len__() > word2.__len__():
        print ("the longest word is" +word3)

input1=input ("Write 3 words; I will print the longest word. Word 1: ")
input2=input ("Word 2:")
input3=input ("Word 3:")

long_word(input1, input2,input3)

#print the longest word 2 #打印最长的单词2

def long_word (word1,word2,word3):

    word1a=(len(word1))
    word2a=(len(word2))
    word3a=(len(word3))
    if word1a > word2a and word1a>word3a:
        print ("the longest word is" + word1)
    elif word2a >word1a and word1a>word3a:
        print ("the longest word is" +word2)
    elif word3a >word1a and word3a > word2a:
        print ("the longest word is" +word3)

input1=input ("Write 3 words; I will print the longest word. Word 1: ")
input2=input ("Word 2:")
input3=input ("Word 3:")

long_word(input1, input2,input3)

In general you can use one of the methods listed in the answers to the question How to measure elapsed time in Python?通常,您可以使用问题How to measure elapsed time in Python的答案中列出的方法之一 . .

In your specific case, I expect the running times of the two functions to be very very similar (the running time is never the same because it depends on what other operations your system is doing) because the expression word1.__len__() is exactly equivalent to len(word1) , so the algorithm is exactly the same.在您的特定情况下,我希望这两个函数的运行时间非常相似(运行时间永远不会相同,因为它取决于您的系统正在执行的其他操作),因为表达式word1.__len__()完全等效到len(word1) ,所以算法是完全一样的。

You should never call __len__ directly (it is not Pythonic), but instead use always len that internally calls __len__ .你永远不应该直接调用__len__ (它不是 Pythonic),而是使用始终在内部调用__len__ len

In python, it is not the best practice to usemagic methods directly.在python中,直接使用魔术方法并不是最佳实践。

And yes, if you want to compare code you can use timeit module as suggested by @mark是的,如果你想比较代码,你可以按照@mark 的建议使用timeit模块

Also, there are other ways of comparing 3 words, one example below:此外,还有其他比较 3 个单词的方法,如下示例:

#example data
word1 = "hello"
word2 = "hi"
word3 = "awsome"

# creating a list of example data
words = [word1, word2, word3]

#finding the max length of word
max_length = max(list(map(len, words)))

# iterating and printing max length word
for word in words:
    if len(word) == max_length:
        print(f"longest word - {word}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM