我想使用同态(paillier)加密来加密300个数字的列表。这在我的笔记本上大约需要3000毫秒,在我的覆盆子pi上需要更长的时间。我想加快速度,所以我尝试使用多线程:
from multiprocessing.dummy import pool as ThreadPool
def test_1_performance_of_encryption(self):
print("Test: Blind encryption performance test")
print("-----------------------------")
print()
# Only blinds are encrypted
for y in range(0, ((NUMBER_OF_RUNS//SENSOR_SAMPLES_PER_BLIND)*NUM_OF_DATA_PROCESSORS)):
print("Round {}:".format(y+1))
print("Encrypting {} blinds...".format(NUM_OF_SENSOR_SAMPLES))
encrypted_blinds.clear()
millis_start = int(round(time.time() * 1000))
for x in range(0, NUM_OF_SENSOR_SAMPLES):
encrypted_blinds.append(public_keys[0].encrypt(blinds[x]))
millis_end = int(round(time.time() * 1000))
time_elapsed_enc.append(millis_end - millis_start)
print("Time elapsed: {}ms".format(time_elapsed_enc[y]))
print("Test finished. Time elapsed:")
print("Min: {} | Max: {} | Avg: {}".format(min(time_elapsed_enc), max(time_elapsed_enc),
(sum(time_elapsed_enc)/len(time_elapsed_enc))))
print()
@profile
def test_1a_performance_of_encryption_multithreaded(self):
print("Test: Blind encryption performance test with {} threads".format(NUM_OF_THREADS))
for y in range(0, ((NUMBER_OF_RUNS//SENSOR_SAMPLES_PER_BLIND)*NUM_OF_DATA_PROCESSORS)):
print("Round {}:".format(y+1))
print("Encrypting {} blinds...".format(len(blinds)))
millis_start = int(round(time.time() * 1000))
encrypted_blinds_multithreaded = pool.map(public_keys[0].encrypt, blinds)
millis_end = int(round(time.time() * 1000))
time_elapsed_enc_multithread.append(millis_end - millis_start)
print("Time elapsed: {}ms".format(time_elapsed_enc_multithread[y]))
print("Test finished. Time elapsed:")
print("Min: {} | Max: {} | Avg: {}".format(min(time_elapsed_enc_multithread), max(time_elapsed_enc_multithread),
(sum(time_elapsed_enc_multithread) / len(time_elapsed_enc_multithread))))
print()
但是,这两项测试的完成时间或多或少完全相同。虽然单线程方法使用100%的一个核心,但多线程版本使用所有这些,但仍然会创建一个1的负载(等于1核心,100%)。我在这里做错了吗?我已经阅读了这个问题并得到了答案:Python multiprocessing.Pool() doesn't use 100% of each CPU,但是我不相信这里的原因是进程间通信,因为它完全以1的负载结算会非常奇怪......
编辑:我使用multiprocessing.dummy
代替multiprocessing
。我认为这使用了多个线程而不是进程,并且由于名为GIL(全局解释器锁)的东西,多个线程无法在Python中并行执行。我通过将multiprocessing.dummy
更改为multiprocessing
来修复此问题。我现在有n个进程和100%的CPU使用率。