import time
import threading
import multiprocessing

max_process = 4
max_thread = max_process


def fun(n, n2):
    # cpu密集型
    for i in range(0, n):
        for j in range(0, int(n * n * n * n2)):
            t = i * j


def thread_main(n2):
    start = time.time()

    thread_list = []
    for i in range(0, max_thread):
        t = threading.Thread(target=fun, args=(50, n2))
        thread_list.append(t)

    print(' [+] much thread start')
    for i in thread_list:
        i.start()
    for i in thread_list:
        i.join()
    print(' [-] much thread use ', time.time() - start, 's')


def process_main(n2):
    start = time.time()

    p = multiprocessing.Pool(max_process)
    for i in range(0, max_process):
        p.apply_async(func=fun, args=(50, n2))
    print(' [+] much process start')
    p.close()  # 关闭进程池
    p.join()  # 等待所有子进程完毕
    print(' [-] much process use ', time.time() - start, 's')


if __name__ == '__main__':

    print("[++]When n=50,n2=0.1:")
    thread_main(0.1)
    process_main(0.1)

    print("[++]When n=50,n2=1:")
    thread_main(1)
    process_main(1)

    print("[++]When n=50,n2=10:")
    thread_main(10)
    process_main(10)
