python学习(11) - list转tensor的不同方式对比
在做实验的时候,Run win提示'Creating a tensor from a list of numpy.ndarrays is extremely slow',也就是说将list转tensor速度是很慢的,为了探究这里说的extremely是多大程度的慢,我尝试用以下几种方式将list转tensor并进行对比。
先说结论:
如果list中有ndarrays,则选择list->ndarrays->tensor更快;
如果list中没有ndarrays,则选择list->tensor更快。
1.list->tensor(注:list中的元素不含numpy.ndarrays)
import numpy as np
import torch
import time
l = [i for i in range(50000000)] # 五千万
stime = time.time()
torch.tensor(l)
etime = time.time()
print(f'用时: {etime-stime}s')
>>>用时: 1.713258981704712s
2. list->tensor(注:list中的元素含numpy.ndarrays)
l = [np.ones(1) for i in range(50000000)] # 五千万
stime = time.time()
torch.tensor(l)
etime = time.time()
print(f'用时: {etime-stime}s')
用时: 41.249605894088745s
3. list->numpy.ndarrays->tensor(注:list中的元素不含numpy.ndarrays)
import numpy as np
import torch
import time
l = [i for i in range(50000000)] # 五千万
stime = time.time()
torch.tensor(np.array(l))
etime = time.time()
print(f'用时: {etime-stime}s')
>>>用时: 2.480342388153076s
4.list->numpy.ndarraays->tensor(注:list中的元素含numpy.ndarrays)
l = [np.ones(1) for i in range(50000000)] # 五千万
stime = time.time()