# How to get forward time

**URL:** <https://discuss.pytorch.org/t/how-to-get-forward-time/25158>\
**Category:** Uncategorized\
**Created:** [September 14, 2018, 3:07am UTC](https://discuss.pytorch.org/t/how-to-get-forward-time/25158 "2018-09-14T03:07:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shl1993](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@shl1993](https://discuss.pytorch.org/u/shl1993)\
**Post date:** [September 14, 2018, 3:07am UTC](https://discuss.pytorch.org/t/how-to-get-forward-time/25158/1 "2018-09-14T03:07:02Z")

</div>

```auto
import torch
import time
from torchvision.models import vgg16

size = 512
num = 1000

net = vgg16().features.cuda()

print(net)

x = torch.zeros((1, 3, size, size)).cuda()

cost = 0

for i in range(num):
    t0 = time.time()
    y = net(x)
    t1 = time.time()
    cost += t1 - t0

cost = cost / num * 1000

print("input size is {}, test {} times, average spend time {:.2f}ms".format(size, num, cost))

```

My GPU is GTX1060  
when set num=10, i got 1.58ms  
when set num=1000 i got 33.64ms  
how to understand?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [September 14, 2018, 9:15am UTC](https://discuss.pytorch.org/t/how-to-get-forward-time/25158/2 "2018-09-14T09:15:18Z")

</div>

As CUDA calls are asynchronously, you would have to synchronize before starting and stopping the timer:

```python
torch.cuda.synchronize()
t0 = time.time()
y = net(x)
torch.cuda.synchronize()
t1 = time.time()

```

Could you add it and try it again?
