import multiprocessing
import time
from itertools import product
out_file = open("test.txt", 'w')
P = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p','q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',]
N = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
M = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
c = int(input("Insert the number of digits you want: "))
n = int(input("If you need number press 1: "))
m = int(input("If you need upper letters press 1: "))
i = []
if n == 1:
P = P + N
if m == 1:
P = P + M
then = time.time()
def worker():
for i in product(P, repeat=c): #check every possibilities
k = ''
for z in range(0, c): #
k = k + str(i[z]) # print each possibility in a txt without parentesis or comma
out_file.write(k + '\n') #
out_file.close()
now = time.time()
diff = str(now - then) # To see how long does it take
print(diff)
worker()
time.sleep(10) # just to check console
코드는 모든 가능성을 확인하고 test.txt
파일에 인쇄합니다. 작동하지만 실제로 어떻게 속도를 높일 수 있는지 이해할 수 없습니다. 내가 쿼드 코어 CPU에서 1 코어를 사용하는 것을 보았 기 때문에 멀티 스레딩은 어떻게 작동 할 지 모른다고 생각했습니다. 도와주세요. 내 영어로 죄송합니다. 이탈리아 출신입니다.멀티 스레딩 프로그램 (파이썬)을 만들어야합니다
스레드를 사용하면 속도가 향상되지 않습니다. GIL은 한 번에 하나의 활성 스레드 만 허용합니다. –
@StefanoBorini 거의. CPython은 한 번에 하나의 스레드 만 파이썬 바이트 코드 *를 실행할 수 있습니다. –
어쨌든 속도를 낼 수 없습니까? – Andreawu98