How to get the greatest number in a list of numbers using multiprocessing

2024/11/18 9:42:16

I have a list of random numbers and I would like to get the greatest number using multiprocessing.

This is the code I used to generate the list:

import random
randomlist = []
for i in range(100000000):n = random.randint(1,30000000)randomlist.append(n)

To get the greatest number using a serial process:

import timegreatest = 0 # global variabledef f(n):global greatestif n>greatest:greatest = nif __name__ == "__main__":global greatestt2 = time.time()greatest = 0for x in randomlist:f(x)    print("serial process took:", time.time()-t2)print("greatest = ", greatest)

This is my try to get the greatest number using multiprocessing:

from multiprocessing import Pool
import timegreatest = 0 # the global variabledef f(n):global greatestif n>greatest:greatest = nif __name__ == "__main__":global greatestgreatest = 0t1 = time.time()p = Pool() #(processes=3) result = p.map(f,randomlist)p.close()p.join()print("pool took:", time.time()-t1)print("greatest = ", greatest)

The output here is 0. It is clear that there is no global variable. How can I fix this without affecting the performance?

Answer

As suggested by @Barmar, divide your randomlist into chunk then process local maximum from each chunk and finally compute global maximum from local_maximum_list:

import multiprocessing as mp
import numpy as np
import random
import timeCHUNKSIZE = 10000def local_maximum(l):m = max(l)print(f"Local maximum: {m}")return mif __name__ == '__main__':randomlist = np.random.randint(1, 30000000, 100000000)start = time.time()chunks = (randomlist[i:i+CHUNKSIZE]for i in range(0, len(randomlist), CHUNKSIZE))with mp.Pool(mp.cpu_count()) as pool:local_maximum_list = pool.map(local_maximum, chunks)print(f"Global maximum: {max(local_maximum_list)}")end = time.time()print(f"MP Elapsed time: {end-start:.2f}s")

Performance

It's very interesting how the creation of the random list impacts the performance of multiprocessing

Scenario 1:
randomlist = np.random.randint(1, 30000000, 100000000)
MP Elapsed time: 1.63sScenario 2:
randomlist = np.random.randint(1, 30000000, 100000000).tolist()
MP Elapsed time: 6.02sScenario 3
randomlist = [random.randint(1, 30000000) for _ in range(100000000)]
MP Elapsed time: 7.14sScenario 4:
randomlist = list(np.random.randint(1, 30000000, 100000000))
MP Elapsed time: 184.28sScenario 5:
randomlist = []
for _ in range(100000000):n = random.randint(1, 30000000)randomlist.append(n)
MP Elapsed time: 7.52s
https://en.xdnf.cn/q/118694.html

Related Q&A

python pandas yahoo stock data error

i am try to pullout intraday aapl stock data by yahoo. but there problem i facing with my program..import pandas as pd import datetime import urllib2 import matplotlib.pyplot as plt get = http://chart…

Web Scraping Stock Ticker Price from Yahoo Finance using BeautifulSoup

Im trying to scrape Gold stock ticker from Yahoo! Finance. from bs4 import BeautifulSoup import requests, lxmlresponse = requests.get(https://finance.yahoo.com/quote/GC=F?p=GC=F) soup = BeautifulSoup(…

How to convert the radius from meter to pixel?

I have a camera with these specs:full resolution 1280x1024 pixel size 0.0048mm focal length 8 mmI need to detect a ball in this image. It is 4 meters away and its radius is 0.0373 meter. How to convert…

Calculting GPA using While Loop (Python)

A GPA, or Grade point Average, is calculated by summing the grade points earned in a student’s courses and then dividing by the total units. The grade points for an individual course are calculated by…

Return function that modifies the value of the input function

How can I make a function that is given a function as input and returns a function with the value tripled. Here is some pseudo code for what Im looking for. Concrete examples in Python or Scala would b…

how to access the list in different function

I have made a class in which there are 3 functions. def maxvalue def min value def getActionIn the def maxvalue function, I have made a list of actions. I want that list to be accessed in def getaction…

Replacing numpy array with max value [duplicate]

This question already has answers here:numpy max vs amax vs maximum(4 answers)Closed 2 years ago.I have an array, a = np.array([[0,9,8],[5,6,4]])how to replace the each array in axis 1 with the max val…

Finding the max and min in dictionary as tuples python

so given this dictionary im trying to find the max value and min value {Female :[18,36,35,49,19],Male :[23,22,6,36,46]}the output should be in tuples for example key: (min,max)Female: (18,49) Male: (6,…

Flask sqlalchemy relations across multiple files

Im new to Flask Sqlalchemy and I want to declare multiple models and relate them to each other, I followed the example in the documentation but I keep getting this error sqlalchemy.exc.InvalidRequestEr…

Parsing XML with Pykml

I have the following xml file I got from QGIS<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.…