1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| %matplotlib inline import math import time import numpy as np import torch from d2l import torch as d2l
n=10000 a=torch.ones([n]) b=torch.ones([n])
class Timer: def __init__(self): self.times=[] self.start() def start(self): self.tik=time.time() def stop(self): self.times.append(time.time()-self.tik) return self.times[-1] def avg(self): return sum(self.times)/len(self.times) def sum(self): return sum(self.times) def cumsum(self): return np.array(self.times).cumsum().tolist()
|