serpent
Following this small benchmark experiment (https://blog.fefe.de/?ts=a2689de5), I wrote a short Elixir script to calculate the counts of distinct words in a text read from stdin. Other language versions are available (http://ptrace.fefe.de/wp/) as well as instructions to create the input file and some performance results (http://ptrace.fefe.de/wp/timings2019.txt).
#!/usr/bin/env elixir
# setup input stream for reading and tokenisation
IO.stream(:stdio, :line)
|> Stream.flat_map(&(String.split(&1)))
# ingest stream, count words, sort result
|> Enum.reduce(%{}, fn x, acc -> Map.update(acc, x, 1, &(&1 + 1)) end)
|> Map.to_list
|> Enum.sort(fn {_, a}, {_, b} -> b < a end)
# print it beautifully
|> Enum.each(fn {word, count} ->
IO.puts(String.pad_leading(Integer.to_string(count), 8) <> " " <> word)
end)
It performs pretty badly:
% time ./exwp.exs < words.txt > /dev/null
./exwp.exs < words.txt > /dev/null 137.20s user 8.48s system 110% cpu 2:11.49 total
Comparing with the Ruby version from above link:
% time ./wp.rb < words.txt > /dev/null
./wp.rb < words.txt > /dev/null 21.54s user 0.19s system 99% cpu 21.750 total
Sure, this is not a typical use case and BEAM is not exactly famous for its string performance – but it’s fun to play around, right? Now I’m wondering, is it just my script? Is there room for improvement?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Welcome to the forum,
There is a better implementation
PS: You might have a look at Flow as the example is about counting words in a textfile.
jola
Could you share a link to the input?
I tried generating it and
josevalim
To be precise, what is slow in this case is not the string processing, but the fact that Map is a immutable data-structure. For example, using
:ets.update_counter/4should speed up things considerably. The documentation for Flow talks about this too.serpent
For me
did work. I put it on my server as well (http://teralink.net/\~serpent/words.txt).
jola
For reference, your original code took 1:54 total on my machine.
Simply running
takes around 35s on my machine. So just reading from IO and splitting is already slower than the full ruby implementation.
Switching to
IO.binstreamspeeds it up from 35 to 25s , so that’s one step. The difference is that we don’t have UTF8 support, which I assume most of the implementations don’t bother with anyway.Next lets take a look at @josevalim’s suggestion of using ETS
This runs in 42s on my machine, so we’ve made a big improvement from 114s. We can shave another 2s off by replacing the last line with building an iolist.
But we’re still not really using the concurrency available to Elixir. @kokolegorille suggested using Flow, which lets us easily run on more than one core. Using almost the exact example posted in the documentation for Flow I ran
which finishes in 21s. That means we’ve caught up with ruby. But I dropped ETS, so lets bring it back.
This doesn’t run any faster than the previous Flow version, still clocking in at 21s (saw some variance, maybe 20-22s, not surprising since I’m using around 7/8 virtual cores and I have lots of apps running). I’m not really sure why it’s not faster, considering we’re avoiding updating a
Map. I’m curious if there’s a smarter way of reading the ETS table, maybe even in a sorted fashion?Finally, the original post claims to allow VMs startup time before measuring, which your examples of using
timedoes not. Timing the code from start to finish gives me an actual time of at least 0.6s less than whattimereports. If you want to get more accurate than that you’ll need to use a proper testing harness like GitHub - bencheeorg/benchee: Easy and extensible benchmarking in Elixir providing you with lots of statistics! · GitHubps fun note about ETS, without
{:write_concurrency, true}the final version takes 120s instead of 21spps noticed I forgot an unnecessary
.partitionin the final version. Removing it yields a run time of ~17s.dimitarvp
Not sure if we can get sorting but using Erlang’s relatively new :counters module should be even faster than ETS in this case.
josevalim
Great summary @jola!
Note that If you are using ETS, you don’t need partition. So the previous version was also correct. In fact, if you don’t need partition for this example, you likely don’t need flow either and IO.binstream+Task.async_stream will most likely be fine.
For reading ETS, you can also use :ets.tab2list to get the whole table as a list, but I am not sure if it makes a big difference.
counters require an index (integer) as a key, so you would need a way to convert words into integers which may end-up offsetting the benefits you would get from counterss.
joaoevangelista
Forenote: I’m on Windows using Git Bash so my IO is garbage
I tried some of the code, first I used the
words.txtfile that @serpent provided and since I’m dumb and I don’t know how to feed fromcattoelixirI usedFile.stream!/1sorry!But the points that I want to shared that at least occurred on my machine was :
String.split/3on:|> Stream.flat_map(&String.split(&1, " "))is faster thanString.split/1for splitting at spacesI came to the loose conclusion that iterating and strings are not the weak point but the IO devices are?
Here is the code:
Run with:
$ elixir wc.exsjosevalim
You can do this to verify. Replace this:
by this:
jola
Eked out a another 3s improvement by compiling a pattern for
String.split. Now down to 14s run time. Also, if I switch toFile.stream!instead ofIO.binstreamI’m down to 12s, but I’ll stick with the original code since that was part of the challenge.in comparison the Flow-less synchronous version runs in 25s