4

I have a code that looks like this:

%%time
import time
time.sleep(3)

When I executed this cell in the jupyter I got this output:

CPU times: user 791 µs, sys: 1.47 ms, total: 2.27 ms
Wall time: 3 s

My problem is that when I am putting sleep(3) shouldn't the total time be 3sec instead of 2.27ms.

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
ashish14
  • 650
  • 1
  • 8
  • 20
  • Possible duplicate of [Simple way to measure cell execution time in ipython notebook](https://stackoverflow.com/questions/32565829/simple-way-to-measure-cell-execution-time-in-ipython-notebook) – Isma Apr 16 '19 at 06:39

3 Answers3

8
CPU times: user 791 µs, sys: 1.47 ms, total: 2.27 ms
Wall time: 3 s

CPU times shows the time you used your CPU.
Wall time shows the real time elapsed since the beginning of the cell. This is the time you are interested in.

Try the following to see the difference:

%%time
time.sleep(3) #Assuming the time module was already imported

You never used your CPU, hence CPU Times is 0s

ggrelet
  • 1,071
  • 7
  • 22
3

Another way to calculate execution time

You can use this code to calculate total execution time for multiple cells or entire code

from datetime import datetime 

start_time = datetime.now() 

import time
time.sleep(3) 

print('Time elapsed (hh:mm:ss.ms) {}'.format(datetime.now() - start_time))

output:

Time elapsed (hh:mm:ss.ms) 0:00:03.008139
Suhas_Pote
  • 3,620
  • 1
  • 23
  • 38
2

I suggest you to use the jupyter extension Execute Time

enter image description here

To install it, read the documentation or have a look to that post: https://stackoverflow.com/a/50384459/3733974

mbh86
  • 6,078
  • 3
  • 18
  • 31