简体   繁体   中英

Python3.6 threading is not slow as expected on Windows (CPython interpreter)

I wrote some code to test the affect of GIL to multithreaded application with python 3.6 on Windows (8 cores). It was a simple CPU-bound task. The expectation is that the single threaded version is faster than the multithreaded one but the reality is the other way around. Please help me explain. Thank you.

import time
import concurrent.futures

NUMBERS = [5000000 + x for x in range(20)]

def cpu_bound(number):
    return sum(i * i for i in range(number))


def find_sums(numbers):
    for number in numbers:
        cpu_bound(number)
        

def find_sums_threading(numbers):
    with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
        executor.map(cpu_bound, numbers)


def run():
    start_time = time.time()
    find_sums(NUMBERS)
    duration = time.time() - start_time
    print(f"Duration {duration} seconds")


def run_threading():
    start_time = time.time()
    find_sums_threading(NUMBERS)
    duration = time.time() - start_time
    print(f"Duration {duration} seconds")


run()
run_threading()

In a multithreaded Python program, all the threads use a single CPU. All the threads share the same address space, which means that all the threads can access the same set of objects. Splitting a CPU-bound program across several threads will not result in a performance improvement because no additional CPU cycles have been brought into play. Performance will get slightly worse because of the overhead involved in task switching.

In a multiprocessing Python program, child processes use other CPU cores if they are available. Different process do not share an address space, which means that special methods must be used to pass data from one process to another. This data-passing is an overhead for a multiprocessing application, but it is often the case that splitting a CPU-bound program across multiple processes will improve performance. More than one CPU core means more computing power is available to the program.

Python supports both mechanisms in its standard library, under the title of "Concurrent Execution."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM