简体   繁体   中英

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.

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.

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


Based on this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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