import concurrent.futures as cf, multiprocessing ; from time import time # Import python modukles for multiprocessing and timimg. def multiply_on_a_CPUs_core(multiplications_on_this_CPU_core): # Define a function that multiplies floating point numbers on each CPU core. for n in range(multiplications_on_this_CPU_core): # Look for the best number of multiplications that fit in this CPU's cache. x = n * 1.2345 # Woo hoo! Multiply! return # Return from the multiplication function. print("CPU cores used, total multiplications, multiplications per second") # Describe the output columns. for cpu_cores in range(1, 1 + multiprocessing.cpu_count()): # Test if more cores are faster. for exponent_of_batch_size_of_floating_point_numbers_to_multiply_on_a_CPU in range(30): # Consider increasing the range of exponents until multiplications per second peaks. total_multiplications = 2**exponent_of_batch_size_of_floating_point_numbers_to_multiply_on_a_CPU # Convert the exponent to total mutiplications to bench mark. futures = [] # Create an empty python list for concurrent.futures. start_time = time() # Note when multiplications started. with cf.ProcessPoolExecutor() as executor: # I don't know why this would be needed. for i in range(cpu_cores): # For each CPU core being used... futures.append(executor.submit(multiply_on_a_CPUs_core, total_multiplications // cpu_cores )) # assign its share of of the mutiplications to it. print('%.i, %.i, %i' % (cpu_cores, total_multiplications, total_multiplications / ( time() - start_time ) )) # Report the results.