https://github.com/qqwy/elixir_benchmark_list_swap
Benchmarks what implementation of List.swap is the fastest
https://github.com/qqwy/elixir_benchmark_list_swap
Last synced: 2 months ago
JSON representation
Benchmarks what implementation of List.swap is the fastest
- Host: GitHub
- URL: https://github.com/qqwy/elixir_benchmark_list_swap
- Owner: Qqwy
- Created: 2016-07-07T14:14:14.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-07-07T14:20:54.000Z (over 9 years ago)
- Last Synced: 2025-02-22T18:30:03.729Z (8 months ago)
- Language: Elixir
- Size: 3.91 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ListSwap
This module contains four implementations of the `swap` function, that swaps two elements in a list.
### Naive Swap
iex>ListSwap.naive_swap([1,2,3,4,5,6], 3, 5)
[1, 2, 6, 4, 5, 3]This is a simple approach using `Enum.fetch!` and `List.keyreplace`. It is however not very fast.
### Split Swap
iex>ListSwap.split_swap([1,2,3,4,5,6], 3, 5)
[1, 2, 6, 4, 5, 3]This is an approach where the input list is split multiple times and combined in a different order.
### Map Swap
iex>ListSwap.map_swap([1,2,3,4,5,6], 3, 5)
[1, 2, 6, 4, 5, 3]
iex>ListSwap.map_swap2([1,2,3,4,5,6], 3, 5)
[1, 2, 6, 4, 5, 3]These two approaches convert the list to an index-keyed map first, so we can harness the power of maps to do the replacement quicker. But converting the list to a map takes some time. The first one uses `Enum.into %{}` to do this, while the second one uses a custom recursion method and `Map.new` in an attempt to be more efficient.
## Execution of the Benchmark
mix deps.get
mix deps.compile
mix bench