Here 10 is a code snippet showing how to synchronize the code before starting and stopping the timer as well as using some warmup iterations to get more stable results.
If you are using cudnn.benchmark = True, the first iteration for a new input shape will be slower, as cudnn will profile different kernels and select the fastest one, so warmup iterations would be necessary.