简体   繁体   English

如何提高python脚本的速度?

[英]How can I improve the speed my python script?

I wrote this really nice python code that will go through a whole list of name or words and stuff and then filters some things and then saves them, but then I had a big issue and that was the speed, I tryed to fix the issue by doing somethings, but it didn't make any different. 我写了这个非常不错的python代码,它将遍历名称,单词和东西的整个列表,然后过滤一些内容然后保存它们,但是那时我遇到了一个大问题,那就是速度,我试图通过做某事,但没什么不同。 So is there a way to improve the speed of this program? 那么有没有办法提高程序速度呢?

#!/usr/bin/env python 2.7.12
#
#
#
from os import system
from time import sleep
from os  import path
from sys  import exit


def Main():
    global wordlist,Name
    system('clear')
    wordlist = raw_input("Enter The listfile Name: ")  
    if not path.exists(wordlist):
        system('clear');exit('\'%s\' Not Found' %wordlist)

    system('clear');Name = raw_input("Enter Name To Save As: ") 
    if path.exists(Name):
        system('clear');exit('\'%s\' Already Exists' %Name)



def Generate(lst,save):
    words = open(lst,'r')
    Wordlist = open(save,'a')
    Bank=[]
    for word in words:
        if int(len(str(word))-1) > 7 and int(len(str(word))-1) <= 10 and str(word).lower() not in Bank:
            system('clear')
            print '[+] Modding: %s' %word
            Wordlist.write(str(word).upper())
            Wordlist.write(str(word).lower())
            Wordlist.write(str(word).title())
            Bank.append(str(word).lower())




if __name__=='__main__':        
    Main();
    Generate(wordlist,Name)
    system('clear');exit('[-] Done')

Calling clear for every word slows down your program dramatically. 每个单词都调用clear会大大降低程序速度。 Remove these calls. 删除这些电话。 Your program is IO bound, so speeding it up, means speeding up writing. 您的程序受IO限制,因此加快速度就意味着加快编写速度。 Use a SSD. 使用SSD。 Use sets instead of lists: 使用集合而不是列表:

#!/usr/bin/env python
import os
import sys

def generate(lst, save):
    with open(lst, 'r') as words, open(save, 'a') as output:
        bank = set()
        for word in words:
            if 7 < len(word) - 1 <= 10 and word.lower() not in bank:
                print '[+] Modding: %s' % word
                output.write(word.upper())
                output.write(word.lower())
                output.write(word.title())
                bank.append(word.lower())

def main():
    wordlist = raw_input("Enter The listfile Name: ")  
    if not path.exists(wordlist):
        return "'%s' Not Found" % wordlist

    name = raw_input("Enter Name To Save As: ") 
    if path.exists(Name):
        return "'%s' Already Exists" % name
    generate(wordlist, name)
    return '[-] Done'

if __name__=='__main__':        
    sys.exit(main())

What you're trying to do, making permutations of a wordlist is simply expensive. 您要做的是,对单词表进行排列是非常昂贵的。

However 2 tips: 但是有2个提示:

Be careful with system 小心系统

system, much like in C, has a lot of overhead. 与C语言中的系统非常相似,系统也有很多开销。 In the innerloop it gets called a lot of times. 在内部循环中,它被调用了很多次。 This will seriously slow it down. 这将严重降低速度。

for word in words:
        if int(len(str(word))-1) > 7 and int(len(str(word))-1) <= 10 and str(word).lower() not in Bank:
            system('clear')
            ...

Python 蟒蛇

CPython is simply not that fast, libraries like numba and interpreters like Pypy do improve upon this greatly. CPython并没有那么快,诸如numba之类的库和诸如Pypy之类的解释器确实在此方面有了很大的改进。 However if you want to make something that runs as fast as possible, go C. 但是,如果您想使某些东西能够尽快运行,请执行C。

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

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