如何解决提高python的性能
因此,我想出了一个加密类,该类基本上可以将字符串中的字符保存为dict键和该键中列表的索引。它使用的dict在我的3个dict之间是随机的,它指定在字符串的beginnig中使用哪个dict。如果字典中没有字符,则将其转换为二进制并使用“&”指定。
例如,“§2,¿0,#1,?1,#4,%4”是“狗”
“§2”告诉解密方法应该使用哪个命令。 (在本例中为dict3)
所以我的问题是,如何才能提高我的代码性能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 = {"$": ["~","^": ["t","!": ["n","*": ["x",")","#": ["*","%": ["+","@": ["a","~": ["=","ç"]
}
dict3 = {"~": ["~","#": ["m","!": [")","$": ["x","t","*": ["*","ç"],"%": [".","?": ["s","^": ["3","=","¿": ["a","ã"]
}
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,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,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))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。