0

Through a python program, sending a command to specific device and that device is responding on the behalf of the command. Now I have to calculate timing between send and receive (means how much time taking to response of the command ).

Ex.

device ip - 10.0.0.10
transmitting 'L004' command through our local system to 10.0.10.
Receving 'L' response from 10.0.0.10. 

So now I have to calculate time difference between start time and end time.

Please provide an API through that I can calculate.

3 Answers 3

2
import time

t1 = time.time()

# some time-demanding operations

t2 = time.time()

print "operation took around {0} seconds to complete".format(t2 - t1)

time.time() returns the current unix timestamp as a float number. Store this number at given points of your code and calculate the difference. You will get the time difference in seconds (and fractions).

Sign up to request clarification or add additional context in comments.

Comments

2

The timeit standard module makes it easy to do this kind of task.

Comments

0

Just Use "timeit" module. It works with both Python 2 And Python 3

import timeit

start = timeit.default_timer()
#ALL THE PROGRAM STATEMETNS
stop = timeit.default_timer()
execution_time = stop - start

print("Program Executed in "+execution_time) #It returns time in sec

It returns in Seconds and you can have your Execution Time. Simple but you should write these in Main Function which starts program execution. If you want to get the Execution time even when you get error then take your parameter "Start" to it and calculate there like

`def sample_function(start,**kwargs): try: #your statements Except: #Except Statements stop = timeit.default_timer() execution_time = stop - start

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.