https://www.hackerrank.com/challenges/big-sorting/problem
Sort a list of long numbers, all in all up to a million digits might be used.
Erlang can deal with long numbers. See solution_slow.erl.
But it turns out this approach is too slow to tackle all test cases.
So I tried to use strings instead of numbers, which needs its own comparison function to replace the standard lexicographic order.
This was fast enough. See solution.erl.
I did not know what made the first solution slow. I suspected the sorting. So I wanted to profile the code.
I downloaded a testcase from hackerrank and found out from the Erlang FAQ how to execute the programs using standard input and output in the shell.
The easiest way to profile in this setup seemed using erlang:timestamp/0 and timer:now_diff/2.
See solution_slow_tc.erl and solution_tc.erl and run-slow-tc.sh and run-tc.sh.
op | time [s] |
---|---|
read : | 5.039 |
sort : | 0.0 |
write: | 31.06 |
op | time [s] |
---|---|
read : | 0.249 |
sort : | 0.125 |
write: | 1.279 |
So my guess was wrong, it is not sorting which is slower for integers than strings, but the I/O. Quite a difference.