Efficiency of .to(device) in pytorch
For achieving maximum efficiency, which command would be more efficient? 

x = torch.randn(100, 100).to(device)
x = torch.randn(100, 100, device = device)


Is there a benefit to using one versus the other when doing heavy tensor operations? I was told one of them is less efficient, but can't properly figure how to compare the two.

I'm assuming the second is better as it directly exports to device instead of first creating a tensor and then having to transfer it to device.

well, I'm no expert in this. Here's the timing result for you.

%timeit torch.randn(100, 100).to(device)

The slowest run took 12.65 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 129 µs per loop


%timeit torch.randn(100, 100, device = device)

The slowest run took 88.54 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 11.6 µs per loop


P.S. I executed both these commands on Google Colab.
