[Shootout-list] Diff to hash.erlang (makes loc shorter)
Einar Karttunen
ekarttun@cs.helsinki.fi
Sun, 13 Jun 2004 19:02:35 +0300
This is a MIME-formatted message. If you see this text it means that your
E-mail software does not support MIME-formatted messages.
--=_courier-1533-1087142562-0001-2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Hello
Here is a diff to make the hash.erl shorter. I think I could optimize
the times a little bit... What may be the key? An atom, a string, a
binary, a number? Please test first as allways.
- Einar Karttunen
--=_courier-1533-1087142562-0001-2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="hash.diff"
--- orig/hash.erlang 2004-06-13 18:54:25.000000000 +0300
+++ hash.erlang 2004-06-13 18:57:07.000000000 +0300
@@ -16,29 +16,15 @@
io:format("~w~n", [Count]).
% halt(0).
-
-format_hex(X) ->
- format_hex(X, []).
-
-format_hex(0, []) -> list_to_atom("0");
-format_hex(0, Hex) -> list_to_atom(Hex);
-format_hex(X, Hex) ->
- N = X band 16#f,
- if
- N < 10 -> format_hex(X bsr 4, [N+$0 | Hex]);
- true -> format_hex(X bsr 4, [(N-10)+$a | Hex])
- end.
-
doinserts(N, N, H) -> ok;
doinserts(I, N, H) ->
- Hx = format_hex(I),
- ets:insert(H, { Hx, I }),
+ ets:insert(H, { erlang:integer_to_list(I, 16), I }),
doinserts(I+1, N, H).
dolookups(0, C, H) -> C;
dolookups(I, C, H) ->
- Nx = list_to_atom(integer_to_list(I)),
+ Nx = integer_to_list(I),
case ets:lookup(H, Nx) of
[] -> dolookups(I-1, C, H);
Found -> dolookups(I-1, C+1, H)
--=_courier-1533-1087142562-0001-2--