I wanted to drop the last element of a list. I didn't want a queue, I simply wanted to remove the last character of a string. Specifically I wanted to remove the \n from ref: refs/heads/master\n that comes back when reading the .git/HEAD file.
Code listing for trim.erl module:
-module(trim).
-export([string_strip_right/1, reverse_tl_reverse/1, bench/0]).
bench() -> [nbench(N) || N <- [1,1000,1000000]].
nbench(N) -> {N, bench(["a" || _ <- lists:seq(1,N)])}.
bench(String) ->
{{string_strip_right,
lists:sum([
element(1, timer:tc(trim, string_strip_right, [String]))
|| _ <- lists:seq(1,1000)])},
{reverse_tl_reverse,
lists:sum([
element(1, timer:tc(trim, reverse_tl_reverse, [String]))
|| _ <- lists:seq(1,1000)])}}.
string_strip_right(String) -> string:strip(String, right, $\n).
reverse_tl_reverse(String) ->
lists:reverse(tl(lists:reverse(String))).
Benchmark transcript:
Erlang R13B04 (erts-5.7.5) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.5 (abort with ^G)
1> trim:bench().
[{1,{{string_strip_right,11261},{reverse_tl_reverse,1000}}},
{1000,
{{string_strip_right,55131},{reverse_tl_reverse,17915}}},
{1000000,
{{string_strip_right,79881856},
{reverse_tl_reverse,119920353}}}]
2>
No comments:
Post a Comment