0

So, recently i've been interested in checking out how long it takes for someone or something to finish a task. (For example, how long it takes for someone to answer a prompt) So i've been thinking of how to track the time taken. I've thought of it before, but failed miserably and didn't try again until now. This is what I tried before, but it didn't work properly for obvious reasons (because time.sleep waits for a few seconds but doesn't tell you how long it took). (I'm trying to track someone's time, not dictate it) But recently, I thought of a system that works but isn't precise. It goes something like this, and it works, but it's not precise. Because if I take 5.37 seconds to answer the prompt, it shows that I took 5 seconds. It works, but I was just wondering if it was possible to be more precise..

Sipher_
  • 71
  • 9

1 Answers1

2

Use datetime:

import datetime

before = datetime.datetime.now()

a = input()

after = datetime.datetime.now()

diff = after-before

diff.total_seconds()

Returns:

2.426867
Anton vBR
  • 18,287
  • 5
  • 40
  • 46