I have a script which outputs the time taken by a command as follows:
real 112m5.559s
user 0m0.002s
sys 0m0.000s
I am parsing the output from the script using python. I want to represent the real time in seconds format. I have the parsed output in a variable real_time
.
Here's how I am currently converting it to seconds format.
def gettime(x):
m,s = map(float,x[:-1].split('m'))
return 60 * m + s
real_time = "112m5.559s"
gettime(real_time)
Is there any other way to do this? (Probably a one liner which is more efficient)
timeit
tests for the answers given below :
import pandas as pd
def gettime(x):
m,s = map(float,x[:-1].split('m'))
return 60 * m + s
real_time = "112m5.559s"
%timeit gettime(real_time)
%timeit int(real_time.split("m")[0]) * 60 + float(real_time.split("m")[1][:-1])
%timeit pd.to_timedelta(real_time).total_seconds()
%timeit sum(float(x) * 60 ** i for i, x in enumerate(reversed(real_time[:-1].split('m'))))
Output
1.27 µs ± 55.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1.39 µs ± 104 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
15.8 µs ± 714 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
2.63 µs ± 446 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Current method seems to be the efficient one. @bigbounty's answer comes close to being both efficient and one-liner.