Solved by qnl in post #4 
              
              
                Finally, I found solution. It’s x1000 faster than first code.(25 ms ± 649 µs ) 
%%timeit
import torch
        
class SparseArray:
    def __init__(self):
        self.values = []
        self.indexs = []
        self.row = -1
        self.col = 0
        self.maxCol = 0
        
    def clear(self):…