美文网首页
Parallel for loop

Parallel for loop

作者: 勤奋的红狐狸 | 来源:发表于2018-11-29 21:15 被阅读0次

    一个简单的例子

    from math import sqrt
    from joblib import Parallel, delayed
    
    # single-core code
    sqroots_1 = [sqrt(i ** 2) for i in range(10)]
    
    # parallel code
    sqroots_2 = Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
    

    带输入的函数的并行

    from joblib import Parallel, delayed
    import multiprocessing
         
    # a single task
    inputs = range(10) 
    def processInput(i):
        return i * i
     
    num_cores = multiprocessing.cpu_count()
    results = Parallel(n_jobs=num_cores)(delayed(processInput)(i) for i in inputs)
    

    常见的几种编程语言的for循环并行的用法https://blog.dominodatalab.com/simple-parallelization/

    相关文章

      网友评论

          本文标题:Parallel for loop

          本文链接:https://www.haomeiwen.com/subject/efjqcqtx.html