View Source Benchee.Conversion.Duration (Benchee v1.1.0)

Unit scaling for duration converting from microseconds to minutes and others.

Only Benchee plugins should use this code.

Link to this section Summary

Functions

The most basic unit in which measurements occur.

Finds the best unit for a list of durations. By default, chooses the most common unit. In case of tie, chooses the largest of the most common units.

Converts a value for a specified %Unit or unit atom and converts it to the equivalent of another unit of measure.

Converts a value of the given unit into the desired unit, returning only the value not the unit.

Formats a number as a string, with a unit label. To specify the unit, pass a tuple of {value, unit_atom} like {1_234, :second}

Scales a duration value in nanoseconds into a larger unit if appropriate

Scales a duration value in nanoseconds into a value in the specified unit

Get a unit by its atom representation. If handed already a %Unit{} struct it just returns it.

Link to this section Functions

The most basic unit in which measurements occur.

examples

Examples

iex> Benchee.Conversion.Duration.base_unit.name
:nanosecond
Link to this function

best(list, opts \\ [strategy: :best])

View Source

Finds the best unit for a list of durations. By default, chooses the most common unit. In case of tie, chooses the largest of the most common units.

Pass [strategy: :smallest] to always return the smallest unit in the list. Pass [strategy: :largest] to always return the largest unit in the list.

examples

Examples

iex> Benchee.Conversion.Duration.best([23, 23_000, 34_000, 2_340_000]).name
:microsecond

iex> Benchee.Conversion.Duration.best([23, 23_000, 34_000_000, 2_340_000_000, 3_450_000_000]).name
:second

iex> Benchee.Conversion.Duration.best([23, 23_000, 34_000, 2_340_000], strategy: :smallest).name
:nanosecond

iex> Benchee.Conversion.Duration.best([23, 23_000, 34_000, 2_340_000_000], strategy: :largest).name
:second
Link to this function

convert(number_and_unit, desired_unit)

View Source

Converts a value for a specified %Unit or unit atom and converts it to the equivalent of another unit of measure.

examples

Examples

iex> {value, unit} = Benchee.Conversion.Duration.convert({90, :minute}, :hour) iex> value 1.5 iex> unit.name :hour

Link to this function

convert_value(arg, desired_unit)

View Source

Converts a value of the given unit into the desired unit, returning only the value not the unit.

examples

Examples

iex> Benchee.Conversion.Duration.convert_value({1.234, :second}, :microsecond)
1_234_000.0

iex> Benchee.Conversion.Duration.convert_value({1.234, :minute}, :microsecond)
7.404e7

iex> microseconds = Benchee.Conversion.Duration.convert_value({1.234, :minute}, :microsecond)
iex> {value, _} = Benchee.Conversion.Duration.convert({microseconds, :microsecond}, :minute)
iex> value
1.234

Formats a number as a string, with a unit label. To specify the unit, pass a tuple of {value, unit_atom} like {1_234, :second}

examples

Examples

iex> Benchee.Conversion.Duration.format(45_678.9)
"45.68 μs"

iex> Benchee.Conversion.Duration.format(45.6789)
"45.68 ns"

iex> Benchee.Conversion.Duration.format({45.6789, :millisecond})
"45.68 ms"

iex> Benchee.Conversion.Duration.format {45.6789,
...>   %Benchee.Conversion.Unit{
...>     long: "Milliseconds", magnitude: 1000, label: "ms"}
...>   }
"45.68 ms"

Scales a duration value in nanoseconds into a larger unit if appropriate

examples

Examples

iex> {value, unit} = Benchee.Conversion.Duration.scale(1)
iex> value
1.0
iex> unit.name
:nanosecond

iex> {value, unit} = Benchee.Conversion.Duration.scale(1_234)
iex> value
1.234
iex> unit.name
:microsecond

iex> {value, unit} = Benchee.Conversion.Duration.scale(11_234_567_890_123)
iex> value
3.1207133028119443
iex> unit.name
:hour

Scales a duration value in nanoseconds into a value in the specified unit

examples

Examples

iex> Benchee.Conversion.Duration.scale(12345, :nanosecond)
12345.0

iex> Benchee.Conversion.Duration.scale(12345, :microsecond)
12.345

iex> Benchee.Conversion.Duration.scale(12345, :minute)
2.0575e-7

Get a unit by its atom representation. If handed already a %Unit{} struct it just returns it.

examples

Examples

iex> Benchee.Conversion.Duration.unit_for :hour
%Benchee.Conversion.Unit{
  name:      :hour,
  magnitude: 3_600_000_000_000,
  label:     "h",
  long:      "Hours"
}

iex> Benchee.Conversion.Duration.unit_for(%Benchee.Conversion.Unit{
...>   name:      :hour,
...>   magnitude: 3_600_000_000_000,
...>   label:     "h",
...>   long:      "Hours"
...>})
%Benchee.Conversion.Unit{
  name:      :hour,
  magnitude: 3_600_000_000_000,
  label:     "h",
  long:      "Hours"
}