Measuring elapsed time with the Time module
start_time = time.time()
# your code
elapsed_time = time.time() - start_time
You can also write simple decorator to simplify measurement of execution time of various functions:
import time
from functools import wraps
PROF_DATA = {}
def profile(fn):
@wraps(fn)
def with_profiling(*args, **kwargs):
start_time = time.time()
ret = fn(*args, **kwargs)
elapsed_time = time.time() - start_time
if fn.__name__ not in PROF_DATA:
PROF_DATA[fn.__name__] = [0, []]
PROF_DATA[fn.__name__][0] += 1
PROF_DATA[fn.__name__][1].append(elapsed_time)
return ret
return with_profiling
def print_prof_data():
for fname, data in PROF_DATA.items():
max_time = max(data[1])
avg_time = sum(data[1]) / len(data[1])
print "Function %s called %d times. " % (fname, data[0]),
print 'Execution time max: %.3f, average: %.3f' % (max_time, avg_time)
def clear_prof_data():
global PROF_DATA
PROF_DATA = {}
Usage:
@profile
def your_function(...):
...
You can profile more then one function simultaneously. Then to print measurements just call the print_prof_data():
Measuring elapsed time in python
You can use perf_counter
function of time module in Python Standard Library:
from datetime import timedelta
from time import perf_counter
startTime = perf_counter()
CallYourFunc()
finishedTime = perf_counter()
duration = timedelta(seconds=(finishedTime - startTime))
How do I get time of a Python program's execution?
The simplest way in Python:
import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))
This assumes that your program takes at least a tenth of second to run.
Prints:
--- 0.764891862869 seconds ---
How to measure time taken between lines of code in python?
If you want to measure CPU time, can use time.process_time()
for Python 3.3 and above:
import time
start = time.process_time()
# your code here
print(time.process_time() - start)
First call turns the timer on, and second call tells you how many seconds have elapsed.
There is also a function time.clock()
, but it is deprecated since Python 3.3 and will be removed in Python 3.8.
There are better profiling tools like timeit
and profile
, however time.process_time() will measure the CPU time and this is what you're are asking about.
If you want to measure wall clock time instead, use time.time()
.
Python Elapsed Time as Days, Hours, Minutes, Seconds
You could use datetime:
from datetime import datetime as dt
start = dt.fromtimestamp(1588432670)
end = dt.now()
elapsed=end-start
print("Took: %02d:%02d:%02d:%02d" % (elapsed.days, elapsed.seconds // 3600, elapsed.seconds // 60 % 60, elapsed.seconds % 60))
Output:
Took: 33:00:21:49
Measuring elapsed time in Pandas
Welcome to StackOverflow. I think the question you are looking to answer is how to convert the time string to a datetime format without the date portion. Doing so requires only a minor modification to your code.
pd.to_datetime(df['Elapsed Time'], format = '%H:%M:%S').dt.time
Complete code:
import pandas as pd
data_dict = { 'Elapsed Time': ['00:22:05', '00:30:34', '00:30:31', '00:37:19', '00:28:43', '00:22:08'] }
df = pd.DataFrame.from_dict(data_dict)
df['Formatted Time'] = pd.to_datetime(df['Elapsed Time'], format = '%H:%M:%S').dt.time
type(df['Elapsed Time'][0]) # 'str'
type(df['Formatted Time'][0]) # 'datetime.time'
Computing with Time
In order to perform analysis of the data you'll need to convert the time value to something useful, such as seconds. Here I'll present two methods of doing that.
The first method performs manual calculations using the original time string.
def total_seconds_in_time_string(time_string):
segments = time_string.strip().split(':')
# segments: [ 'HH', 'MM', 'SS' ]
# total seconds = (((HH * 60) + MM) * 60) + SS
return (((int(segments[0]) * 60) + int(segments[1])) * 60) + int(segments[2])
df['Total Seconds'] = df['Elapsed Time'].apply(lambda x: total_seconds_in_time_string(x))
type(df['Total Seconds'][0]) # 'numpy.int64'
df['Total Seconds'].mean() # 1713.3333333333333
def seconds_to_timestring(secs):
import time
time_secs = time.gmtime(round(secs))
return time.strftime('%H:%M:%S', time_secs)
avg_time_str = seconds_to_timestring(df['Total Seconds'].mean())
print(avg_time_str) # '00:28:33'
The second method would be the more Pythonic solution using the datetime
library.
def total_seconds_in_time(t):
from datetime import timedelta
return timedelta(hours=t.hour, minutes=t.minute, seconds=t.second) / timedelta(seconds=1)
df['TimeDelta Seconds'] = df['Formatted Time'].apply(lambda x: total_seconds_in_time(x))
type(df['TimeDelta Seconds'][0]) # 'numpy.float64'
df['TimeDelta Seconds'].mean() # 1713.3333333333333
def seconds_to_timedelta(secs):
from datetime import timedelta
return timedelta(seconds=round(secs))
mean_avg = seconds_to_timedelta(df['TimeDelta Seconds'].mean())
print(mean_avg) # '0:28:33'
Related Topics
How to Improve Performance of This Code
Why Is "Except: Pass" a Bad Programming Practice
What Is a Mixin and Why Is It Useful
How to Convert All Strings in a List of Lists to Integers
How to Create a Text Input Box With Pygame
How to Concatenate Text Files in Python
What Is the Python Keyword "With" Used For
Python Pandas Error Tokenizing Data
Changing One List Unexpectedly Changes Another, Too
Best Way to Return Multiple Values from a Function
Make a Dictionary With Duplicate Keys in Python
Pandas: How to Easily Share a Sample Dataframe Using Df.To_Dict()
Matplotlib/Seaborn: First and Last Row Cut in Half of Heatmap Plot