4 ms·
Here's an example that illustrates the phenomenon. If memory serves me right, index latency is superlinear in dimension count. import time, torch from i
by nuisance-bear 5y ago
Here's an example that illustrates the phenomenon. If memory serves me right, index latency is superlinear in dimension count.
import time, torch
from itertools import product
N = 100
ten = torch.randn(N,N,N)
arr = ten.numpy()
def indexTimer(val):
start = time.time()
for i,j,k in product(range(N), range(N), range(N)):
x = val[i, j, k]
end = time.time()
print('{:.2f}'.format(end-start))
indexTimer(ten)
indexTimer(arr)
- sideshowb 5y agoAh, I'm guessing it's the loop that kills you. Arrays/tensors are supposed to be used as a whole, I imagine e.g. x=val**2 would be much faster. I'm indexing one array with another e.g. x=y[z] to pull out all the values I want at once into another array for processing. And not using a python loop for the expensive part.
- deleted 5y ago[deleted]