简体   繁体   中英

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. 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. Remove these calls. Your program is IO bound, so speeding it up, means speeding up writing. Use a 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:

Be careful with system

system, much like in C, has a lot of overhead. 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. However if you want to make something that runs as fast as possible, go C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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