Python 3.x 提高python的性能

Python 3.x 提高python的性能,python-3.x,windows,encryption,data-structures,Python 3.x,Windows,Encryption,Data Structures,因此,我提出了一个加密类,它基本上可以将字符保存在字符串中,作为dict密钥和该密钥中列表的索引。它使用的dict在我的3个dict之间是随机的,它指定在字符串的开头使用哪个dict。如果一个字符不在dict中,那么它将被转换为二进制,并使用“&”来指定它 例如,“2、?0、?1、?1、?4、%4”是“狗” “§2”告诉解密方法它应该使用哪个dict。(在本例中为第3条) 所以我的问题是,如何才能100%地提高代码的性能?我想让它运行得更快,因为我经常使用加密 import re from ra

因此,我提出了一个加密类,它基本上可以将字符保存在字符串中,作为dict密钥和该密钥中列表的索引。它使用的dict在我的3个dict之间是随机的,它指定在字符串的开头使用哪个dict。如果一个字符不在dict中,那么它将被转换为二进制,并使用“&”来指定它

例如,“2、?0、?1、?1、?4、%4”是“狗”

“§2”告诉解密方法它应该使用哪个dict。(在本例中为第3条)

所以我的问题是,如何才能100%地提高代码的性能?我想让它运行得更快,因为我经常使用加密

import re
from random import choice

class encryption:

    dict1 = {"#": ["~", "6", "7", "8", "9", "z", "^", "á"],
             "~": ["t", "y", "u", "i", "o", "p", "[", "õ"],
             "*": [")", "h", "j", "b", "k", "l", "$", "ã"],
             "!": ["x", "c", "v", "n", "m", " ", "(", "í"],
             "$": ["*", "!", "@", "%", "/", "\\", "'","ã"],
             "@": ["+", "-", "_", "?", "]", ":", ";", "ê"],
             "?": [".", ",", "|", "¦", "¿", "&", "}", "ô"],
             "%": ["=", "<", ">", "s", "d", "f", "#", "ó"],
             "¿": ["3", "4", "5", "q", "w", "e", "r", "é"],
             "^": ["a", "1", "2", "0", "g", '"', "{", "ç"]
             }
    
    dict2 = {"$": ["~", "6", "7", "4", "5", "q", "w", "á"],
             "^": ["t", "y", "u", "i", "/", "\\", "'","ã"],
             "!": ["n", "m", " ", "(", "k", "l", "$", "ã"],
             "*": ["x", "c", "v", ")", "h", "j", "b", "í"],
             "#": ["*", "9", "z", "^", "e", "p", "[", "õ"],
             "%": ["+", "-", "_", "?", "]", ":", ";", "ê"],
             "?": [".", ",", "|", "¦", "¿", "&", "}", "ô"],
             "@": ["a", "1", "2", "0", "d", "f", "#", "ó"],
             "¿": ["3", "8", "!", "@", "%", "o", "r", "é"],
             "~": ["=", "<", ">", "s", "g", '"', "{", "ç"]
             }
    
    dict3 = {"~": ["~", "_", "?", "]", "9", "z", "^", "á"],
             "#": ["m", " ", "(", "i", "o", "p", "[", "õ"],
             "!": [")", "h", "j", "b", "k", "l", "$", "ã"],
             "$": ["x", "c", "v", "n", "t", "y", "u", "í"],
             "*": ["*", "!", "@", "%", "/", '"', "{", "ç"],
             "@": ["+", "-", "6", "7", "8", ":", ";", "ê"],
             "%": [".", ",", "2", "0", "g", "&", "}", "ô"],
             "?": ["s", "d", "f", "#", "w", "e", "r", "ó"],
             "^": ["3", "4", "5", "q", "=", "<", ">", "é"],
             "¿": ["a", "1", "|", "¦", "¿", "\\", "'","ã"]
             }

    def binary(self, text):
        return format(ord(text), "b")

    def notBinary(self, text):
        return chr(int(text, 2))

    def encrypt(self, text):
        dicts = [self.dict1, self.dict2, self.dict3]
        self.dicty = choice(dicts)
        
        for n, d in enumerate(dicts):
            if d == self.dicty:
                self.guider = "§%s," % n

        self.output = ""
        self.char = [i for i in text]
        self.char2 = [i for i in text]
        for key, values in self.dicty.items():
            i = 0
            for char in values:
                i2 = 0
                for char2 in self.char:
                    if char.lower() == char2.lower():
                        if char != char2:
                            toAdd = f"↑{key}{i}"
                            self.char[i2] = toAdd
                        else:
                            toAdd = f"{key}{i}"
                            self.char[i2] = toAdd

                    i2 += 1
                i += 1

        i = 0
        for c1, c2 in zip(self.char, self.char2):
            if c1 == c2:
                toAdd = "&" + self.binary(self.char[i])
                self.char[i] = toAdd

            i += 1
        
        
        self.output = "%s%s" % (self.guider, ",".join(self.char))

        return self.output

    def decrypt(self, text):
        dicts = [self.dict1, self.dict2, self.dict3]
        self.guider = text[0:3]
        text = text.replace(self.guider, "")
        self.guider = int(self.guider[1])
        self.dicty = dicts[self.guider]
        self.output = ""
        self.char = []
        self.char2 = []
        if re.search(",", text):
            self.char = text.split(',')
            self.char2 = text.split(",")
        else:
            self.char.append(text)
            self.char2.append(text)

        i = 0

        for char in self.char:
            if re.search("↑", char) and not re.search("&", char):
                self.char[i] = char.replace("↑", "")
                self.char[i] = self.dicty[char[1]][int(char[2])].upper()

            elif not re.search("&", char):
                self.char[i] = self.dicty[char[0]][int(char[1])]

            i += 1

        i = 0

        for c1, c2 in zip(self.char, self.char2):
            if c1 == c2:
                toAdd = self.notBinary(self.char[i].replace("&", ""))
                self.char[i] = toAdd

            i += 1

        for i in self.char:
            self.output += i
        return self.output


if __name__ == "__main__":
    from time import time
    from statistics import mean
    e = encryption()
    while True:
        Input = input("INPUT:")
        times = []
        for i in range(1, 10001):
            start = time()
            encrypted = e.encrypt(Input)
            end = time()
            times.append(end - start)
        
        print(mean(times))

重新导入
从随机进口选择
类别加密:
格言1={“#”:[“~”,“6”,“7”,“8”,“9”,“z”,“^”,“á”],
“~”:[“t”,“y”,“u”,“i”,“o”,“p”,“[”,“õ”],
“*”:[”、“h”、“j”、“b”、“k”、“l”、“美元”、“ã”],
“!”:[“x”,“c”,“v”,“n”,“m”,“m”,“(“,”í“]),
"$": ["*", "!", "@", "%", "/", "\\", "'","ã"],
"@": ["+", "-", "_", "?", "]", ":", ";", "ê"],
"?": [".", ",", "|", "¦", "¿", "&", "}", "ô"],
“%:[“=”、“、”s”、“d”、“f”、“#”、“ó”],
“?:[“3”、“4”、“5”、“q”、“w”、“e”、“r”、“é”],
“^”:[“a”,“1”,“2”,“0”,“g”,“''”,“{”,“ç”]
}
格言2={“$”:[“~”、“6”、“7”、“4”、“5”、“q”、“w”、“á”],
“^”:[“t”,“y”,“u”,“i”,“/”,“\\”,“'”,“ã”],
“!”:[“n”、“m”、“k”、“l”、“美元”、“ã”],
“*”:[“x”、“c”、“v”、“h”、“j”、“b”、“í”],
“#”:[“*”,“9”,“z”,“^”,“e”,“p”,“[”,“õ”],
"%": ["+", "-", "_", "?", "]", ":", ";", "ê"],
"?": [".", ",", "|", "¦", "¿", "&", "}", "ô"],
“@”:[“a”、“1”、“2”、“0”、“d”、“f”、“#”、“ó”],
“?”:[“3”,“8”,“!”,“@”,“百分比”,“o”,“r”,“é”],
“~”:[“=”,”,“s”,“g”,“'”,“{”,“ç”]
}
dict3={“~”:[“~”,“”,“?”,“]”,“9”,“z”,“^”,“á”],
“#”:[“m”,“i”,“o”,“p”,“[”,“õ”],
“!”:[”、“h”、“j”、“b”、“k”、“l”、“美元”、“ã”],
“$”:[“x”、“c”、“v”、“n”、“t”、“y”、“u”、“í”],
"*": ["*", "!", "@", "%", "/", '"', "{", "ç"],
"@": ["+", "-", "6", "7", "8", ":", ";", "ê"],
“%:[”、“、”、“2”、“0”、“g”、“和”、“}”、“ô”],
“?”:[“s”、“d”、“f”、“#”、“w”、“e”、“r”、“ó”],
“^”:[“3”,“4”,“5”,“q”,“=”,“,”,“é”],
“
}
def二进制文件(自身,文本):
返回格式(ord(文本),“b”)
def NOTBARINAL(自身,文本):
返回chr(int(text,2))
def加密(自我,文本):
dicts=[self.dict1,self.dict2,self.dict3]
self.dicty=选择(dicts)
对于枚举中的n,d(dicts):
如果d==自述:
self.guider=“§%s,”%n
self.output=“”
self.char=[i代表文本中的i]
self.char2=[i代表文本中的i]
对于键,self.dicty.items()中的值:
i=0
对于值中的字符:
i2=0
对于self.char中的char2:
如果char.lower()==char2.lower():
如果char!=char2:
toAdd=f“↑{key}{i}”
self.char[i2]=toAdd
其他:
toAdd=f“{key}{i}”
self.char[i2]=toAdd
i2+=1
i+=1
i=0
对于压缩中的c1、c2(self.char、self.char2):
如果c1==c2:
toAdd=“&”+self.binary(self.char[i])
self.char[i]=toAdd
i+=1
self.output=“%s%s”%(self.guider,,”.join(self.char))
返回自输出
def解密(自我,文本):
dicts=[self.dict1,self.dict2,self.dict3]
self.guider=文本[0:3]
text=text.replace(self.guider,“”)
self.guider=int(self.guider[1])
self.dicy=dicts[self.guider]
self.output=“”
self.char=[]
self.char2=[]
如果重新搜索(“,”文本):
self.char=text.split(',')
self.char2=text.split(“,”)
其他:
self.char.append(文本)
self.char2.append(文本)
i=0
对于self.char中的char:
如果重新搜索(“↑,char)而不是重新搜索(&),char):
self.char[i]=char.replace(“↑", "")
self.char[i]=self.dicty[char[1][int(char[2])].upper()
elif不重新搜索(&),字符):
self.char[i]=self.dicty[char[0]][int(char[1])]
i+=1
i=0
对于压缩中的c1、c2(self.char、self.char2):
如果c1==c2:
toAdd=self.notBinary(self.char[i].替换(&,“”)
self.char[i]=toAdd
i+=1
对于self.char中的i:
自输出+=i
返回自输出
如果名称=“\uuuuu main\uuuuuuuu”:
从时间导入时间
从统计数据看进口意味着什么
e=加密()
尽管如此:
输入=输入(“输入:”)
时间=[]
对于范围(11001)内的i:
开始=时间()
加密=e.encrypt(输入)
结束=时间()
times.append(结束-开始)
打印(平均(次))