Abhishek
Abhishek

Reputation: 21

execution time in python

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.

Upvotes: 0

Views: 297

Answers (3)

Ravi Kumar
Ravi Kumar

Reputation: 802

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

Upvotes: 0

Bastien Léonard
Bastien Léonard

Reputation: 61713

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

Upvotes: 2

eumiro
eumiro

Reputation: 212885

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).

Upvotes: 2

Related Questions