简体   繁体   English

如何使用多处理获取数字列表中的最大数字

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

I have a list of random numbers and I would like to get the greatest number using multiprocessing .我有一个随机数列表,我想使用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 time

greatest = 0 # global variable

def f(n):
    global greatest
    if n>greatest:
        greatest = n

if __name__ == "__main__":
    global greatest

    t2 = time.time()
    greatest = 0

    for 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 time

greatest = 0 # the global variable

def f(n):
    global greatest
    if n>greatest:
        greatest = n

if __name__ == "__main__":
    global greatest
    greatest = 0
    t1 = 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.这里的输出是0,很明显没有全局变量。 How can I fix this without affecting the performance?如何在不影响性能的情况下解决此问题?

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 :正如@Barmar建议,将您randomlist分割成块,然后处理从每个块,最后计算全球最大的局部最大local_maximum_list

import multiprocessing as mp
import numpy as np

CHUNKSIZE = 10000

def local_maximum(l):
    m = max(l)
    print(f"Local maximum: {m}")
    return m

if __name__ == '__main__':
    randomlist = np.random.randint(1, 30000000, 100000000)
    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)}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在数字列表中找到最大的数字 - Find the greatest number in a list of numbers 编写一个程序来使用python查找列表中的最大数? - Write a program to find the greatest number in a list using python? 两个数之间的最大公约数,返回最大数 - Greatest Common Divisor between two numbers, returning the greatest number 您如何计算列表中的最大重复次数? - How do you calculate the greatest number of repetitions in a list? 如何从特定索引之前的列表中找到最大的数字? - How to find the greatest number from a list before a specific index? 在 Django 模型中使用 Greatest 函数时如何获取注释中的相关对象列表 - How to get list of related object in annotion when using Greatest function in django models 有没有办法从随机数中找到最大的数? - is there a way to find the greatest number from random numbers? 获取加起来为数字“n”的数字列表(使用数字重复)——如何考虑多次使用相同数字的情况? - Get a list of numbers that adds up to a number "n" (using repetition of number) - how would one consider the case of using same number multiple times? Python-在不使用max()的情况下找到3个数字中的最大2个 - Python - find greatest 2 of 3 numbers WITHOUT using max() 如何在 python 中获取具有给定数量因子的可能数字列表? - how to get list of possible numbers with given number of factors in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM