-
-
Save ryanwinchester/90620bece9f046561136de9615707bf9 to your computer and use it in GitHub Desktop.
1 billion row challenge in a SINGLE EXPRESSION!
This file contains hidden or 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
Mix.install([ | |
{:explorer, "~> 0.8.0"} | |
]) | |
filepath = "./measurements.txt" | |
defmodule Challenge do | |
import Explorer.Series | |
require Explorer.DataFrame, as: DF | |
def run(filepath) do | |
DF.from_csv!( | |
filepath, | |
delimiter: ";", | |
header: false, | |
eol_delimiter: "\n", | |
lazy: true, | |
dtypes: [column_1: :category, column_2: {:f, 32}] | |
) | |
|> DF.rename(["station", "measurement"]) | |
|> DF.group_by("station") | |
|> DF.summarise(min: min(measurement), mean: mean(measurement), max: max(measurement)) | |
|> DF.mutate(min: round(min, 1), mean: round(mean, 1), max: round(max, 1)) | |
|> DF.sort_by(station) | |
|> DF.collect() | |
|> DF.to_rows() | |
|> Enum.map_join(", ", fn %{"station" => station, "min" => min, "mean" => mean, "max" => max} -> | |
"#{station}=#{min}/#{mean}/#{max}" | |
end) | |
end | |
end | |
Code.ensure_loaded(Challenge) | |
# Time the execution. | |
{time_us, result} = | |
:timer.tc(fn -> | |
Challenge.run(filepath) | |
end) | |
# Print results and time. | |
IO.puts(result) | |
IO.puts("Time: " <> to_string(time_us / 1_000_000) <> "s") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Time: 6.27903s