def test():
"Stupid test function"
L = []
for i in range(100):
L.append(i)
if __name__=='__main__':
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print t.timeit()
import time
from time import strftime
from datetime import datetime
from time import gmtime
def start_time_():
#import time
start_time = time.time()
return(start_time)
def end_time_():
#import time
end_time = time.time()
return(end_time)
def Execution_time(start_time_,end_time_):
#import time
#from time import strftime
#from datetime import datetime
#from time import gmtime
return(strftime("%H:%M:%S",gmtime(int('{:.0f}'.format(float(str((end_time-start_time))))))))
start_time = start_time_()
# your code here #
[i for i in range(0,100000000)]
# your code here #
end_time = end_time_()
print("Execution_time is :", Execution_time(start_time,end_time))
t0 = time.perf_counter()
fun()
t1 = time.perf_counter()
print(t1-t0)
# and if you really want your answer in minutes:
print(f"In minutes: {(t1-t0)/60}")
import atexit
import re
import time
def time_script() -> None:
"""Local namespace to keep starting time as a free variable."""
starting_time = 0.0
@atexit.register
def time_script_enclosed() -> None:
"""Time your scripts easily. Run in the very beginning of the script."""
nonlocal starting_time
if not starting_time:
starting_time = time.perf_counter()
return print(
time.strftime("%H:%M:%S", time.localtime()) + ": Starting script..."
)
# Formatting with proper inflection of plurals
runtime = time.strftime(
"%#H hours, %#M minutes and %#S seconds",
time.gmtime(time.perf_counter() - starting_time),
)
for old, new in (
(r"^0 hours, ", ""),
(r"^1 hours", "1 hour"),
(r"\b0 minutes and ", ""),
(r"\b1 minutes", "1 minute"),
(r"\b1 seconds", "1 second"),
(r"(?: and|,) 0 seconds", ""),
(r"^0 seconds", "less than a second"),
):
runtime = re.sub(old, new, runtime)
# Make the 0-second or 0-minute situation sound more natural
if ("second" in runtime) != ("minute" in runtime):
runtime = runtime.replace(", ", " and ")
return print(f"The script took {runtime} to run.")
time_script_enclosed()