Skip to content

Instantly share code, notes, and snippets.

View alco's full-sized avatar
🇺🇦

Oleksii Sholik alco

🇺🇦
View GitHub Profile
@alco
alco / test.exs
Last active August 29, 2015 14:04
defmodule TasteTest do
use ExUnit.Case
setup context do
{:ok, Map.put(context, :test, String.to_atom("#{context[:test]} (#{context[:group]})"))}
end
test "the truth" do
assert 1 + 1 == 2
end
defmodule R do
def sample_file_inefficient(path, count) do
s = File.stream!(path, [], :line)
sample = Enum.take(s, count) |> List.to_tuple
Stream.drop(s, count)
|> Stream.with_index
|> Enum.reduce(sample, fn {line, index}, sample ->
case random(index) do
r when r < count -> put_elem(sample, r, line)
f = fn x -> {:ok, {x, [1,x,3]}} end
import M
f.(2) |> match!({:ok, {!x, _}})
#=> 2
f.(2) |> match!({:ok, {x, [1,!x,3]}})
#=> 2
f.(2) |> match!({:ok, {13, [1,!x,3]}})
@alco
alco / match.exs
Last active August 29, 2015 14:04
import M
File.read("match.exs") |> match({:ok, bin}, bin) |> String.length
#=> 156
s = """
id;name;value
1;foo;hi
2;bar;bye
"""
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mach/mach_time.h>
#include <stdint.h>
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
@alco
alco / dup_bench.exs
Last active August 29, 2015 14:04
reduplicate
Benchfella.start duration: 1.0
defmodule StringDuplicateBench do
use Benchfella
Enum.each([100, 1000, 10000, 100000], fn n ->
@str "1234567890"
@n n
bench "binary copy #{n}" do
Benchfella.start duration: 1.0, format: :machine, verbose: true
defmodule StringSlice do
use Benchfella
Enum.each([100, 1000, 10000], fn n ->
@str String.duplicate("hello world", n)
@len String.length(@str)
bench "negative both #{n}" do
@alco
alco / results
Last active August 29, 2015 14:03
# master
StringSlice.slice left half: 100 20628.67 µs/op
StringSlice.slice right half: 50 33514.14 µs/op
StringSlice.slice almost all: 50 40865.84 µs/op
StringSlice.slice -1: 50 63981.30 µs/op
# PR
StringSlice.slice -1: 10000000 0.60 µs/op
StringSlice.slice left half: 100 15272.15 µs/op
StringSlice.slice right half: 50 29207.42 µs/op
** (ArgumentError) argument error
:erlang.length(Kernel)
(elixir) lib/kernel/typespec.ex:494: Kernel.Typespec.type_to_signature/1
(elixir) lib/kernel/typespec.ex:286: anonymous fn/3 in Kernel.Typespec.defines_type?/3
(stdlib) lists.erl:1223: :lists.any/2
(elixir) lib/kernel/typespec.ex:289: Kernel.Typespec.defines_type?/3
lib/porcelain/process.ex:16: (module)
BenchfellaBench.binary test 10: 10000000 0.13 µs/op
BenchfellaBench.binary test 100: 10000000 0.13 µs/op
BenchfellaBench.binary test: 10000000 0.13 µs/op
BenchfellaBench.binary test 1000: 10000000 0.13 µs/op
BenchfellaBench.range test: 100000 26.93 µs/op
BenchfellaBench.range test 10: 10000 293.43 µs/op
BenchfellaBench.range test 100: 500 3312.59 µs/op
BenchfellaBench.range test 1000: 50 32802.00 µs/op