# Does torch.tensor([1, 2, 3, 4]) copy data twice?

I’m playing with different ways of converting a Python list of integer to a PyTorch array. The normal way is `torch.tensor([1, 2, 3, 4], dtype=torch.int64)`. However, I find that using Python’s array library can be much faster: `torch.frombuffer(array("q", [1, 2, 3, 4]))`.

Performance micro benchmark, just for how to move a Python list to pytorch tensor:

``````import array
import torch

print("N\tlist\tlist_with_array\tarray")

for N in [100, 1000, 10000, 100000, 1000000]:
list_data = list(range(N))
array_data = array.array('q', list_data)

def create_from_list():
tensor = torch.tensor(list_data, dtype=torch.int64)
return tensor

def create_from_list_with_array():
tensor = torch.frombuffer(array.array("q", list_data), dtype=torch.int64)
return tensor

def create_from_array():
tensor = torch.frombuffer(array_data, dtype=torch.int64)
return tensor

import time

for _ in range(10):
# warmup
create_from_list()
start = time.time()
for _ in range(100):
create_from_list()
elapsed_list = (time.time() - start) / 100 * 1000 # ms

for _ in range(10):
# warmup
create_from_list_with_array()
start = time.time()
for _ in range(100):
create_from_list_with_array()
elapsed_list_with_array = (time.time() - start) / 100 * 1000 # ms

for _ in range(10):
create_from_array()
start = time.time()
for _ in range(100):
create_from_array()
elapsed_array = (time.time() - start) / 100 * 1000 # ms

print(f"{N}\t{elapsed_list:.3f}\t{elapsed_list_with_array:.3f}\t{elapsed_array:.3f}")
``````
N torch.tensor(list) torch. frombuffer(array(list)) torch.frombuffer(array)
100 0.005 0.002 0.001
1000 0.030 0.014 0.001
10000 0.278 0.137 0.001
100000 2.684 1.347 0.001
1000000 26.654 13.759 0.001

If this is the case, it means `torch.tensor(list)` actually copies twice, right?