简体   繁体   English

如何使用 python 以更快的方式生成序列号列表

[英]how to generate list of sequence number in faster way using python

Anyone can improve processing time to generate list of sequence number?任何人都可以提高处理时间以生成序列号列表? Here is my code and it needs ~ 0.05 second.这是我的代码,它需要 ~ 0.05 秒。

import torch
import time
import random
index = [torch.tensor(660000)]
st = time.time()
allowed = [x for x in range(index[0])]
index = random.sample(allowed, 1000)
print(time.time()-st)

Please advise请指教

thank you谢谢你

This should help with OP specific case, not with "generate list of sequence number" stated in question title.这应该有助于 OP 特定情况,而不是问题标题中所述的“生成序列号列表”。

You do not need to create whole list, you can provide the range, this will work with same speed regardless of range size.您不需要创建整个列表,您可以提供范围,无论范围大小如何,这都将以相同的速度工作。

index = random.sample(range(index[0]), 1000)

On my machine it is ~100x faster for range size 1 million在我的机器上,范围大小为 100 万的速度要快约 100 倍


Based on this answer基于这个答案

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

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