cengal.time_management.timer_precision.versions.v_1.timer_precision
1#!/usr/bin/env python 2# coding=utf-8 3 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space> 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18from typing import Tuple, Sequence 19from collections import deque 20from math import sqrt 21from cengal.time_management.repeat_for_a_time import Tracer 22from cengal.time_management.cpu_clock_cycles import perf_counter 23 24""" 25Module Docstring 26Docstrings: http://www.python.org/dev/peps/pep-0257/ 27""" 28 29__author__ = "ButenkoMS <gtalk@butenkoms.space>" 30__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>" 31__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ] 32__license__ = "Apache License, Version 2.0" 33__version__ = "4.4.1" 34__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>" 35__email__ = "gtalk@butenkoms.space" 36# __status__ = "Prototype" 37__status__ = "Development" 38# __status__ = "Production" 39 40 41def timer_precision(timer_functor=perf_counter, testing_time: float=1.0)->float: 42 time_sum = 0 43 tr = Tracer(testing_time) 44 while tr.iter(): 45 first_time = zero_time = timer_functor() 46 while zero_time == first_time: 47 first_time = timer_functor() 48 second_time = first_time 49 while first_time == second_time: 50 second_time = timer_functor() 51 time_diff = second_time - first_time 52 time_sum += time_diff 53 54 average_time_diff = time_sum / tr.iterations_made 55 return average_time_diff 56 57 58def timer_precision_statistics(timer_functor=perf_counter, testing_time: float=1.0) -> Tuple[float, Sequence[float]]: 59 time_readings = deque() 60 time_sum = 0 61 tr = Tracer(testing_time) 62 while tr.iter(): 63 first_time = zero_time = timer_functor() 64 while zero_time == first_time: 65 first_time = timer_functor() 66 second_time = first_time 67 while first_time == second_time: 68 second_time = timer_functor() 69 time_diff = second_time - first_time 70 time_readings.append(time_diff) 71 time_sum += time_diff 72 73 time_diff_mean = time_sum / tr.iterations_made 74 return time_diff_mean, time_readings 75 76 77def timer_precision_variance(timer_functor=perf_counter, testing_time: float=1.0) -> Tuple[float, float, float, float]: 78 max_deviation = None 79 min_deviation = None 80 time_diff_mean, time_readings = timer_precision_statistics(timer_functor, testing_time) 81 deviation_square_sum = 0 82 for time_reading in time_readings: 83 time_deviation = time_reading - time_diff_mean 84 deviation_square_sum += time_deviation ** 2 85 if max_deviation is None: 86 max_deviation = time_deviation 87 max_deviation = max(max_deviation, time_deviation) 88 if min_deviation is None: 89 min_deviation = time_deviation 90 min_deviation = min(min_deviation, time_deviation) 91 92 variance = deviation_square_sum / len(time_readings) 93 return time_diff_mean, variance, max_deviation, min_deviation 94 95 96def timer_precision_standard_deviation(timer_functor=perf_counter, testing_time: float=1.0 97 ) -> Tuple[float, float, float, float]: 98 time_diff_mean, time_variance, max_deviation, min_deviation = timer_precision_variance(timer_functor, testing_time) 99 return time_diff_mean, sqrt(time_variance), max_deviation, min_deviation 100 101 102def timer_precision_99_95_68(timer_functor=perf_counter, testing_time: float=1.0 103 ) -> Tuple[float, float, float, float, float]: 104 # See: https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule 105 mean, sd, max_deviation, min_deviation = timer_precision_standard_deviation(timer_functor, testing_time) 106 val_68 = mean + sd 107 val_95 = mean + 2 * sd 108 val_99 = mean + 3 * sd 109 return val_99, val_95, val_68, max_deviation, min_deviation
def
timer_precision( timer_functor=<cyfunction perf_counter>, testing_time: float = 1.0) -> float:
42def timer_precision(timer_functor=perf_counter, testing_time: float=1.0)->float: 43 time_sum = 0 44 tr = Tracer(testing_time) 45 while tr.iter(): 46 first_time = zero_time = timer_functor() 47 while zero_time == first_time: 48 first_time = timer_functor() 49 second_time = first_time 50 while first_time == second_time: 51 second_time = timer_functor() 52 time_diff = second_time - first_time 53 time_sum += time_diff 54 55 average_time_diff = time_sum / tr.iterations_made 56 return average_time_diff
def
timer_precision_statistics( timer_functor=<cyfunction perf_counter>, testing_time: float = 1.0) -> Tuple[float, Sequence[float]]:
59def timer_precision_statistics(timer_functor=perf_counter, testing_time: float=1.0) -> Tuple[float, Sequence[float]]: 60 time_readings = deque() 61 time_sum = 0 62 tr = Tracer(testing_time) 63 while tr.iter(): 64 first_time = zero_time = timer_functor() 65 while zero_time == first_time: 66 first_time = timer_functor() 67 second_time = first_time 68 while first_time == second_time: 69 second_time = timer_functor() 70 time_diff = second_time - first_time 71 time_readings.append(time_diff) 72 time_sum += time_diff 73 74 time_diff_mean = time_sum / tr.iterations_made 75 return time_diff_mean, time_readings
def
timer_precision_variance( timer_functor=<cyfunction perf_counter>, testing_time: float = 1.0) -> Tuple[float, float, float, float]:
78def timer_precision_variance(timer_functor=perf_counter, testing_time: float=1.0) -> Tuple[float, float, float, float]: 79 max_deviation = None 80 min_deviation = None 81 time_diff_mean, time_readings = timer_precision_statistics(timer_functor, testing_time) 82 deviation_square_sum = 0 83 for time_reading in time_readings: 84 time_deviation = time_reading - time_diff_mean 85 deviation_square_sum += time_deviation ** 2 86 if max_deviation is None: 87 max_deviation = time_deviation 88 max_deviation = max(max_deviation, time_deviation) 89 if min_deviation is None: 90 min_deviation = time_deviation 91 min_deviation = min(min_deviation, time_deviation) 92 93 variance = deviation_square_sum / len(time_readings) 94 return time_diff_mean, variance, max_deviation, min_deviation
def
timer_precision_standard_deviation( timer_functor=<cyfunction perf_counter>, testing_time: float = 1.0) -> Tuple[float, float, float, float]:
97def timer_precision_standard_deviation(timer_functor=perf_counter, testing_time: float=1.0 98 ) -> Tuple[float, float, float, float]: 99 time_diff_mean, time_variance, max_deviation, min_deviation = timer_precision_variance(timer_functor, testing_time) 100 return time_diff_mean, sqrt(time_variance), max_deviation, min_deviation
def
timer_precision_99_95_68( timer_functor=<cyfunction perf_counter>, testing_time: float = 1.0) -> Tuple[float, float, float, float, float]:
103def timer_precision_99_95_68(timer_functor=perf_counter, testing_time: float=1.0 104 ) -> Tuple[float, float, float, float, float]: 105 # See: https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule 106 mean, sd, max_deviation, min_deviation = timer_precision_standard_deviation(timer_functor, testing_time) 107 val_68 = mean + sd 108 val_95 = mean + 2 * sd 109 val_99 = mean + 3 * sd 110 return val_99, val_95, val_68, max_deviation, min_deviation