Created
October 14, 2025 18:34
-
-
Save nickva/d43b7ffd9997cd251f4573e2be25abec to your computer and use it in GitHub Desktop.
RevId Parsing CouchDB Benchmark: decode_hex vs binary_to_integer(RevId, 16)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -module(bench_parse_revid). | |
| -export([go/0]). | |
| -define(REPEAT, 100). | |
| go() -> | |
| go([10, 100, 500, 1000, 2500]). | |
| go([]) -> | |
| ok; | |
| go([N | Rest]) -> | |
| io:format("* N: ~p~n", [N]), | |
| Revs = [binary:encode_hex(crypto:strong_rand_bytes(16), lowercase) || _ <- lists:seq(1, N)], | |
| Dt1 = erlang:convert_time_unit(revid1(Revs), native, microsecond), | |
| Dt2 = erlang:convert_time_unit(revid2(Revs),native, microsecond), | |
| Lists = [binary_to_list(B) || B <- Revs], | |
| Dt3 = erlang:convert_time_unit(revid1_list(Lists),native, microsecond), | |
| Dt4 = erlang:convert_time_unit(revid2_list(Lists),native, microsecond), | |
| io:format("* b2i + <<_:128>> : ~p~n", [Dt1]), | |
| io:format("* decode_hex : ~p (faster pct:~p)~n", [Dt2, round((Dt1-Dt2)/Dt1*100)]), | |
| io:format("* l2i + <<_:128>> : ~p~n", [Dt3]), | |
| io:format("* l2b + decode_hex : ~p (faster pct:~p)~n", [Dt4, round((Dt3-Dt4)/Dt3*100)]), | |
| io:format("************~n~n", []), | |
| go(Rest). | |
| revid1(Revs) -> | |
| T0 = erlang:monotonic_time(), | |
| lists:foreach(fun(_) -> parse_revid1_int(Revs, []) end, lists:seq(1,?REPEAT)), | |
| round((erlang:monotonic_time() - T0)/?REPEAT). | |
| revid2(Revs) -> | |
| T0 = erlang:monotonic_time(), | |
| lists:foreach(fun(_) -> parse_revid2_int(Revs, []) end, lists:seq(1,?REPEAT)), | |
| round((erlang:monotonic_time() - T0)/?REPEAT). | |
| revid1_list(Revs) -> | |
| T0 = erlang:monotonic_time(), | |
| lists:foreach(fun(_) -> parse_revid1_list_int(Revs, []) end, lists:seq(1,?REPEAT)), | |
| round((erlang:monotonic_time() - T0)/?REPEAT). | |
| revid2_list(Revs) -> | |
| T0 = erlang:monotonic_time(), | |
| lists:foreach(fun(_) -> parse_revid2_list_int(Revs, []) end, lists:seq(1,?REPEAT)), | |
| round((erlang:monotonic_time() - T0)/?REPEAT). | |
| parse_revid1_int([], Acc) -> | |
| Acc; | |
| parse_revid1_int([RevId | Rest], Acc) -> | |
| RevInt = binary_to_integer(RevId, 16), | |
| parse_revid1_int(Rest, [<<RevInt:128>> | Acc]). | |
| parse_revid2_int([], Acc) -> | |
| Acc; | |
| parse_revid2_int([RevId | Rest], Acc) -> | |
| parse_revid2_int(Rest, [binary:decode_hex(RevId) | Acc]). | |
| parse_revid1_list_int([], Acc) -> | |
| Acc; | |
| parse_revid1_list_int([RevId | Rest], Acc) -> | |
| RevInt = list_to_integer(RevId, 16), | |
| parse_revid1_list_int(Rest, [<<RevInt:128>> | Acc]). | |
| parse_revid2_list_int([], Acc) -> | |
| Acc; | |
| parse_revid2_list_int([RevId | Rest], Acc) -> | |
| parse_revid2_list_int(Rest, [binary:decode_hex(list_to_binary(RevId)) | Acc]). |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Erlang 26, Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz, Debian 11