Timer

class improvelib.utils.Timer()

Measures time.

Used in preprocess, train, and infer. Also used in workflows.

Attributes:

startTime

Time at init (start).

endTime

Time when timer_end() is called.

time_diffTime

Difference in time between start and end in seconds.

hoursint

Hours in time_diff, when converted to HH:MM:SS.

minutesint

Minutes in time_diff, when converted to HH:MM:SS. Not total minutes.

secondsint

Seconds in time_diff, when converted to HH:MM:SS. Not total seconds.

time_diff_dictdict

Dictionary with keys ‘hours’, ‘minutes’, ‘seconds’ to express total time elapsed.

timer_end()

Sets attributes end, time_diff, hours, minutes, seconds, time_diff_dict.

Parameters:

None

Returns:

None

display_timer()

Calls timer_end, then both prints and returns the time_diff_dict.

Parameters:

None

Returns:

time_diff_dictdict

Dictionary with keys ‘hours’, ‘minutes’, ‘seconds’ to express total time elapsed.

save_timer()

Ends timer if needed and saves the timer as a JSON file. Additional items can also be saved if passed.

Parameters:

dir_to_savestr, Path

Directory to save the JSON file.

filenamestr, optional

Name of the JSON file, including the ‘.json’. Defaults to ‘runtime.json’.

extra_dictdict, optional

Dictionary to append to the time_diff_dict before saving the file.

Returns:

True : bool

Example

The Timer should be started in main() before the call to run() and saved after, as follows:

timer_preprocess = frm.Timer()
ml_data_outdir = run(params)
timer_preprocess.save_timer(dir_to_save=params["output_dir"],
                            filename='runtime_preprocess.json',
                            extra_dict={"stage": "preprocess"})