Skip to content

Instantly share code, notes, and snippets.

@oestrich
Last active September 3, 2019 15:44
Show Gist options
  • Save oestrich/d192aa37b6ecf299a143b042441de235 to your computer and use it in GitHub Desktop.
Save oestrich/d192aa37b6ecf299a143b042441de235 to your computer and use it in GitHub Desktop.
Benchmark for io data vs strings

Simple Interpolation

Operating System: macOS
CPU Information: Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
Number of Available Cores: 4
Available memory: 8 GB
Elixir 1.8.1
Erlang 21.0.2

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s

Benchmarking iodata...
Benchmarking string...

Name             ips        average  deviation         median         99th %
iodata        6.20 M      161.33 ns  ±4226.50%           0 ns        1000 ns
string        3.68 M      271.40 ns ±11207.73%           0 ns        1000 ns

Comparison:
iodata        6.20 M
string        3.68 M - 1.68x slower +110.08 ns

Complex interpolation

Benchmarking iodata...
Benchmarking string...

Name             ips        average  deviation         median         99th %
iodata        1.75 M        0.57 μs  ±7439.11%           0 μs        0.99 μs
string        0.65 M        1.53 μs   ±875.52%        0.99 μs        1.99 μs

Comparison:
iodata        1.75 M
string        0.65 M - 2.68x slower +0.96 μs
defmodule View do
use Spigot, :view
def render("iodata", %{name: name}) do
~i(Hello, #{name})
end
def render("string", %{name: name}) do
~s(Hello, #{name})
end
def render("iodata-long", %{character: character}) do
~E"""
<%= character.name %>
-----
HP: <%= character.hp %>/<%= character.maxhp %>
SP: <%= character.sp %>/<%= character.maxsp %>
EP: <%= character.ep %>/<%= character.maxep %>
Items:
-------
<%= Enum.map(character.items, &render("item-io", &1)) %>
"""
end
def render("item-io", %{name: name}) do
~i(- #{name})
end
def render("string-long", %{character: character}) do
"""
#{character.name}
-----
HP: #{character.hp}/#{character.maxhp}
SP: #{character.sp}/#{character.maxsp}
EP: #{character.ep}/#{character.maxep}
Items:
-------
#{Enum.map(character.items, &render("item-string", &1))}
"""
end
def render("item-string", %{name: name}) do
~s(- #{name})
end
end
character = %{
name: "Eric",
hp: 50,
maxhp: 50,
sp: 35,
maxsp: 35,
ep: 22,
maxep: 25,
items: [
%{name: "Potion"},
%{name: "Sword"}
]
}
Benchee.run(%{
"iodata" => fn -> View.render("iodata", %{name: "Eric"}) end,
"string" => fn -> View.render("string", %{name: "Eric"}) end
})
Benchee.run(%{
"iodata" => fn -> View.render("iodata-long", %{character: character}) end,
"string" => fn -> View.render("string-long", %{character: character}) end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment