练习程序的性能优化

时间:2019-01-17 20:15:49

标签: python regex algorithm performance lambda

我正在用python编写简单的练习程序。简而言之,需要使用以下分隔符从给定的字符串中分割输入文本: , ; : . ! ( ) " ' \ / [ ] space进入单词列表。然后,我只需要打印小写字母,大写字母和大小写混合的单词。

这是我的解决方法:

import re
import time

text = input()
start_time = time.time()

list_of_words = list(re.findall(r"[\w']+", text))

lower_cases_list = [word for word in list_of_words if word.islower()]
upper_cases_list = [word for word in list_of_words if word.isupper()]
mixed_cases_list = [word for word in list_of_words if not word.islower() and 
not word.isupper()]

print('Lower-case:', ', '.join(lower_cases_list))
print('Mixed-case:', ', '.join(upper_cases_list))
print('Upper-case:', ', '.join(mixed_cases_list))
print("--- %s seconds ---" % (time.time() - start_time))

到目前为止,它可以正常工作,但是在我正在测试任务的平台上,允许的执行时间为0.100秒,在最佳情况下,我的程序的执行时间为0.134秒。谁能帮助我优化代码以获得最佳性能?

2 个答案:

答案 0 :(得分:1)

您要遍历您的单词三遍。通过单个for循环执行一次:

for word in list_of_words:

    if word.islower():
        lower_cases_list.append(word)
    elif word.isupper():
        upper_cases_list.append(word)
    else:
        mixed.append(word)

另外,由于现在您只需要遍历list_of_words一次,实际上您不必列出列表,而只需使用一个生成器即可,从而节省了更多时间:

list_of_words = re.findall(r"[\w']+", text)

答案 1 :(得分:0)

似乎内置库 re 比常规方法(如 .replace() .split()

)慢

使用以下代码:

def h(txt):
    txt = txt.replace(',', r' ')
    txt = txt.replace(';', r' ')
    txt = txt.replace(':', r' ')
    txt = txt.replace('.', r' ')
    txt = txt.replace('!', r' ')
    txt = txt.replace('(', r' ')
    txt = txt.replace(')', r' ')
    txt = txt.replace('"', r' ')
    txt = txt.replace('\'', r' ')
    txt = txt.replace('\\', r' ')
    txt = txt.replace('/', r' ')
    txt = txt.replace('[', r' ')
    txt = txt.replace(']', r' ')

    return txt


def do_all(list_of_words):
    lower_cases_list = []
    upper_cases_list = []
    mixed_cases_list = []

    for word in list_of_words:
        if word.islower():
            lower_cases_list.append(word)
        elif word.isupper():
            upper_cases_list.append(word)
        else:
            mixed_cases_list.append(word)

    print('Lower-case:', ', '.join(lower_cases_list))
    print('Mixed-case:', ', '.join(mixed_cases_list))
    print('Upper-case:', ', '.join(upper_cases_list))

text = h(input()).split()

do_all(text)

执行时间<0.50毫秒。这样问题就解决了。 .split() .replace re