simplebench.si_units moduleπ
Utility functions for handling SI units
- simplebench.si_units.si_scale(unit: str, base_unit: str) float[source]π
Get the SI scale factor for a unit given the base unit.
This method will return the scale factor for the given unit relative to the base unit for SI prefixes ranging from tera (T) to pico (p).
Example:
si_scale('ns', 's')returns1e-9- Parameters:
- Returns:
The scale factor for the given unit.
- Return type:
- Raises:
SimpleBenchValueError β If the SI unit is not recognized, if
base_unitis an empty string, or if theunitdoes not end with thebase_unit.SimpleBenchTypeError β If the
unitorbase_unitargs are not type str.
- simplebench.si_units.si_scale_for_largest( ) tuple[str, float][source]π
Get the scale factor and SI unit for the largest in a sequence of numbers.
The scale factor is the factor that should be applied to the numbers to convert them to the desired unit. The SI unit is the unit that corresponds to the scale factor.
It gives the SI prefix unit and scale for the largest absolute value in the sequence. If all numbers are zero, it returns the base unit and a scale factor of 1.0.
- simplebench.si_units.si_scale_for_smallest( ) tuple[str, float][source]π
Get the scale factor and SI unit for the smallest in a sequence of numbers.
The scale factor is the factor that should be applied to the numbers to convert them to the desired unit. The SI unit is the unit that corresponds to the scale factor.
It gives the SI prefix unit and scale for the smallest non-zero absolute value in the sequence. If all numbers are zero, it returns the base unit and a scale factor of 1.0.
- simplebench.si_units.si_scale_to_unit( ) float[source]π
Get the scale factor to convert a current SI unit to a target SI unit based on their SI prefixes.
Example:
scale_by: float = si_scale_to_unit(base_unit='s', current_unit='s', target_unit='ns')
- Parameters:
- Returns:
The scaling factor to convert the current unit to the target unit.
- Return type:
- Raises:
SimpleBenchValueError β If the SI prefix units are not recognized; if
base_unit,current_unit, ortarget_unitis an empty string; or if the units are not compatible (i.e., do not share the same base unit (i.e. βsecondsβ vs βmetersβ)).SimpleBenchTypeError β If the
unit,base_unit,current_unit, ortarget_unitargs are not type str.
- simplebench.si_units.si_unit_base(unit: str) str[source]π
Guess the base unit from an SI unit.
This assumes that the SI unit is a valid SI unit with an optional SI prefix. If the unit is a single character, it is returned as-is on the assumption that it is already the base unit.
We only check for a single-character SI prefix at the start of the unit from the range of supported SI prefixes (from tera (T) to pico (p)).
This CAN result in false positives if the base unit starts with a character that is also a valid SI prefix. (potential false positives: βmβ (meter), βMβ (mole), βPβ (peta), βTβ (tesla), βGβ (gauss), βkβ (katal), βnβ (newton), βpβ (pascal)). However, these units are uncommon in benchmarking and the benefit of simplicity outweighs the risk of false positives.
We could do a more complex check by looking for known SI base units, but this would require maintaining a list of known base units and would still not be exhaustive.
If no valid SI prefix is found, the unit is assumed to already be the base unit.
Example
si_unit_base('ns')returns's'- Parameters:
unit (str) β The SI unit to get the base unit from.
- Returns:
The base unit.
- Return type:
- Raises:
SimpleBenchValueError β If the unit is an empty string.
SimpleBenchTypeError β If the unit arg is not of type str.