开发者

Why is Python random lib really fast for the first 10M calls [closed]

开发者 https://www.devze.com 2022-12-07 22:46 出处:网络
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 14 hours ago.

Improve this question

I have this script that I run:

import random
import sys
import time

start = time.time()
random_to_choose = [i for i in range(0, 100)]
counter = {i: 0 for i in random_to_choose}


def run_sims():
    cnt = int(sys.argv[1]) ** 6
    while cnt != 0:
        counter[random.choice(random_to_choose)] += 1
        cnt -= 1
    print(counter)


run_sims()
print(开发者_运维知识库time.time()-start)

Result:

python .\random_check.py 10
0.32193899154663086
___________________
python .\random_check.py 20
20.77259874343872

If the 10M calls complete in 0.3s I would expect 20M to complete in around 0.6s, not 20s.

0

精彩评论

暂无评论...
验证码 换一张
取 消