def get_elapsed_time(): """ Is the option for one of the following methods to measure time? * perf_counter, perf_counter(): pc_timestamp() * perf_counter_ns, perf_counter_ns(): pc_timestamp_ns() * process_time, process_time(): pt_timestamp() * process_time_ns, process_time_ns(): pt_timestamp_ns() * time, time.time(): time() * time_ns, time.time_ns(): time_ns() * monotonic, monotonic(): pm_monotonic() * monotonic_ns, monotonic_ns(): pm_monotonic_ns() """ if ("perf_counter" == execution_time_measurement. get_type_current_time_measurement()): # Yes. Use perf_counter() to measure performance/time. current_timestamp = pc_timestamp() elif ("perf_counter_ns" == execution_time_measurement.get_type_current_time_measurement()): # Yes. Use perf_counter_ns() to measure performance/time. current_timestamp = pc_timestamp_ns() elif ("process_time" == execution_time_measurement.get_type_current_time_measurement()): # Yes. Use process_time() to measure performance/time. current_timestamp = pt_timestamp() elif ("process_time_ns" == execution_time_measurement.get_type_current_time_measurement()): # Yes. Use process_time_ns() to measure performance/time. current_timestamp = pt_timestamp_ns() elif ("time" == execution_time_measurement.get_type_current_time_measurement()): # Yes. Use time.time() to measure performance/time. current_timestamp = time.time() elif ("time_ns" == execution_time_measurement.get_type_current_time_measurement()): # Yes. Use time.time_ns() to measure performance/time. current_timestamp = t_ns() elif ("monotonic" == execution_time_measurement.get_type_current_time_measurement()): # Yes. Use monotonic() to measure performance/time. current_timestamp = pm_monotonic() else: """ The default option is: "monotonic_ns". Use monotonic_ns() to measure performance/time. """ current_timestamp = pm_monotonic_ns() # Postcondition. Check if elapsed_time_in_ns > 0. elapsed_time_in_ns = current_timestamp - execution_time_measurement.get_initial_timestamp( ) execution_time_measurement.check_elapsed_time(elapsed_time_in_ns) return elapsed_time_in_ns
def set_initial_timestamp(type_timestamp="monotonic_ns"): execution_time_measurement.type_current_time_measurement = type_timestamp """ Is the option for one of the following methods to measure time? * perf_counter, perf_counter(): pc_timestamp() * perf_counter_ns, perf_counter_ns(): pc_timestamp_ns() * process_time, process_time(): pt_timestamp() * process_time_ns, process_time_ns(): pt_timestamp_ns() * time, time.time(): time.time() * time_ns, time.time_ns(): time.time_ns() * monotonic, monotonic(): pm_monotonic() * monotonic_ns, monotonic_ns(): pm_monotonic_ns() """ if ("perf_counter" == type_timestamp): # Yes. Use perf_counter() to measure performance/time. execution_time_measurement.initial_timestamp = pc_timestamp() elif ("perf_counter_ns" == type_timestamp): # Yes. Use perf_counter_ns() to measure performance/time. execution_time_measurement.initial_timestamp = pc_timestamp_ns() elif ("process_time" == type_timestamp): # Yes. Use process_time() to measure performance/time. execution_time_measurement.initial_timestamp = pt_timestamp() elif ("process_time_ns" == type_timestamp): # Yes. Use process_time_ns() to measure performance/time. execution_time_measurement.initial_timestamp = pt_timestamp_ns() elif ("time" == type_timestamp): # Yes. Use time() to measure performance/time. execution_time_measurement.initial_timestamp = time.time() elif ("time_ns" == type_timestamp): # Yes. Use time_ns() to measure performance/time. execution_time_measurement.initial_timestamp = t_ns() elif ("monotonic" == type_timestamp): # Yes. Use monotonic() to measure performance/time. execution_time_measurement.initial_timestamp = pm_monotonic() else: # The default option is: "monotonic_ns()" execution_time_measurement.initial_timestamp = pm_monotonic_ns()
from time import perf_counter as pc_timestamp from time import perf_counter_ns as pc_timestamp_ns from time import process_time as pt_timestamp from time import process_time_ns as pt_timestamp_ns from time import time_ns as t_ns from time import monotonic as pm_monotonic from time import monotonic_ns as pm_monotonic_ns """ References: + [Chitipothu2013] + [Chitipothu2019] Anand Chitipothu, "Notes from Advaced Python Workshop," Stack Exchange Inc., New York, NY, July 24, 2013. Available online from Anand Chitipothu's Web page.: Blog at: https://anandology.com/blog/notes-from-advaced-python-workshop/ and https://anandology.com/blog/notes-from-advaced-python-workshop.html; November 11, 2020 was the last accessed date. + [ProjectJupyterContributors2019] - Anand "anandology" Chitipothu, Jupyter Notebook Viewer, from Project Jupyter: nbviewer: Jupyter Notebook Viewer. - From: Advanced Python Training at LinkedIn, February 26 - March 1, 2014. - Functional Programming - Advanced Python Workshop * First session - Advanced Python Workshop - Functional Programming - Advanced Python Workshop - Classes - Advanced Python Workshop - Context Managers - Advanced Python Workshop - Meta Classes Has information about Python decorators. """ print("= Experimenting with Python decorators from \cite{Chitipothu2019}.") pc_timestamp_ns()
from time import time_ns as t_ns from time import monotonic as pm_monotonic from time import monotonic_ns as pm_monotonic_ns # ======================================================= """ Perform timing measurements with different methods and determine their elapsed time (or differences). """ print("==================================================") # Time units in seconds (s). initial_timestamp = pc_timestamp() # Time units in nanoseconds (ns). current_timestamp = pc_timestamp_ns() elapsed_time = current_timestamp - initial_timestamp print("initial_timestamp via perf_counter is:", initial_timestamp, ".") print("current_timestamp via perf_counter_ns is:", current_timestamp, ".") print("elapsed_time is:", elapsed_time, ".") print( " Error! Combining time measurements in seconds and nanosecond precision would store their values in different time units." ) # Time units in seconds (s). initial_timestamp = pt_timestamp() # Time units in nanoseconds (ns). current_timestamp = pt_timestamp_ns() elapsed_time = current_timestamp - initial_timestamp print("initial_timestamp via process_time is:", initial_timestamp, ".") print("current_timestamp via process_time_ns is:", current_timestamp, ".")