简体   繁体   English

Numpy 随机数生成器延迟

[英]Numpy random number generator latency

Why is the numpy generation of random numbers so much slower in the case of repeated calls compared to a single function call?为什么与单个 function 调用相比,在重复调用的情况下,随机数的 numpy 生成要慢得多?

Example:例子:

import numpy as np
import timeit

if __name__ == '__main__':


    latency_normal = timeit.timeit('np.random.uniform(size=(100,))', setup = 'import numpy as np')
    latency_normal_loop = timeit.timeit('[np.random.uniform(size=1) for _ in range(100)]', setup = 'import numpy as np')

    rng = np.random.default_rng()

    latency_generator = timeit.timeit('rng.uniform(size=(100,))', setup = 'import numpy as np')
    latency_generator_loop = timeit.timeit('[rng.uniform(size=1) for _ in range(100)]', setup = 'import numpy as np')

    print("latencies:\t normal: [{}, {}]\t generator: [{},{}]".format(latency_normal, latency_normal_loop, latency_generator, latency_generator_loop))

Output: Output:

latencies:       normal: [2.7388298519999807, 31.694285575999857]        generator: [2.6634575979996953,31.0009219450003]

Are there any alternatives that performs better for repeated calls with smaller sample sizes?对于样本量较小的重复调用,是否有任何替代方案表现更好?

Obviously there is a large fixed per-call cost associated with the function call.显然,与 function 调用相关的固定每次调用成本很高。 To work around it, you can make a wrapper that will retrieve a batch of random numbers from numpy (ie 100) in a single call and then return values from this cache.为了解决这个问题,您可以制作一个包装器,在一次调用中从 numpy(即 100)中检索一批随机数,然后从该缓存中返回值。 When the cache gets depleted, it will ask numpy for another 100 numbers, etc.当缓存耗尽时,它会向 numpy 询问另外 100 个数字等。

Or, you can simply use Python's random !或者,您可以简单地使用 Python 的random

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

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