-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Benchee.Statistics do | ||
alias Benchee.Time | ||
|
||
@doc """ | ||
Calculates statistical data based on a series of run times in microseconds. | ||
iex> Benchee.Statistics.statistics([200, 400, 400, 400, 500, 500, 700, 900]) | ||
%{average: 500.0, std_dev: 200.0, ips: 2000.0} | ||
""" | ||
def statistics(run_times) do | ||
total_time = Enum.sum(run_times) | ||
iterations = Enum.count(run_times) | ||
average_time = total_time / iterations | ||
iterations_per_second = iterations_per_second(iterations, total_time) | ||
standard_deviation = standard_deviation(run_times, average_time, iterations) | ||
|
||
%{ | ||
average: average_time, | ||
ips: iterations_per_second, | ||
std_dev: standard_deviation | ||
} | ||
end | ||
|
||
defp iterations_per_second(iterations, time_microseconds) do | ||
iterations / (Time.microseconds_to_seconds(time_microseconds)) | ||
end | ||
|
||
defp standard_deviation(samples, average, iterations) do | ||
total_variance = Enum.reduce samples, 0, fn(sample, total) -> | ||
total + :math.pow((sample - average), 2) | ||
end | ||
variance = total_variance / iterations | ||
:math.sqrt variance | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Benchee.Time do | ||
@seconds_to_microseconds 1_000_000 | ||
|
||
def microseconds_to_seconds(microseconds) do | ||
microseconds / @seconds_to_microseconds | ||
end | ||
|
||
def seconds_to_microseconds(seconds) do | ||
seconds * @seconds_to_microseconds | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule Benchee.StatistcsTest do | ||
use ExUnit.Case | ||
doctest Benchee.Statistics | ||
|
||
end |