Created
March 22, 2015 10:29
-
-
Save maxlapshin/e7a18c997a6bac555171 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env escript | |
%! -env ERL_LIBS deps -pa deps/io_libc/ebin | |
-mode(compile). | |
main([]) -> | |
code:add_pathz("deps/io_libc/ebin"), | |
Time = {{2015,3,21},{17,30,57}}, | |
Count = 100000, | |
{{Y,Mo,D}, {H,Mn,S}} = Time, | |
io:format("native: ~s\n",[io_lib:format("~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0BZ", [Y, Mo, D, H, Mn, S])]), | |
Native = fun Native(0) -> ok; | |
Native(N) -> | |
io_lib:format("~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0BZ", [Y, Mo, D, H, Mn, S]), | |
Native(N-1) | |
end, | |
io:format("nif: ~s\n", [io_libc:format("%04d-%02d-%02dT%02d:%02d:%02dZ", [Y, Mo, D, H, Mn, S])]), | |
Nif = fun Nif(0) -> ok; | |
Nif(N) -> | |
io_libc:format("%04d-%02d-%02dT%02d:%02d:%02dZ", [Y, Mo, D, H, Mn, S]), | |
Nif(N-1) | |
end, | |
io:format("erlang: ~s\n", [iso8601(Time)]), | |
Erl = fun Erl(0) -> ok; | |
Erl(N) -> | |
iso8601(Time), | |
Erl(N-1) | |
end, | |
{T1,_} = timer:tc(fun() -> Native(Count) end), | |
{T2,_} = timer:tc(fun() -> Nif(Count) end), | |
{T3,_} = timer:tc(fun() -> Erl(Count) end), | |
io:format("native: ~p\n", [T1 div Count]), | |
io:format("nif: ~p\n", [T2 div Count]), | |
io:format("erlang: ~p\n", [T3 div Count]), | |
ok. | |
iso8601({{Y,Mo,D}, {H,Mn,S}}) -> | |
list_to_binary([integer_to_list(Y), $-, | |
two_digit_str(Mo), $-, | |
two_digit_str(D), $T, | |
two_digit_str(H), $:, | |
two_digit_str(Mn), $:, | |
two_digit_str(S), $Z]). | |
two_digit_str(X) when X < 10 -> | |
[$0 | integer_to_list(X)]; | |
two_digit_str(X) -> | |
integer_to_list(X). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment