测试结果,自己写的效率要高一些
4> test_io:test(1000)
4> .
{[io,0],[my,0]}
5> test_io:test(10000)
5> .
{[io,2],[my,1]}
6> test_io:test(100000)
6> .
{[io,21],[my,4]}
7> test_io:test(1000000)
7> .
{[io,97],[my,57]}
8> test_io:test(10000000)
8> .
{[io,1596],[my,761]}
9>
1 -module(test_io).
2 -export([test/1, test/0]).
3
4 test() ->
5 test(1).
6
7 test(N) ->
8 IoResult = test_io(N),
9 MyResult = test_my(N),
10 {[io, IoResult],[my, MyResult]}.
11
12 test_io(N) ->
13 StartTime = timestamp_in_millinsec(),
14 [begin
15 {{Y,M,D},{Hour,Min,Sec}}=erlang:localtime(),
16 Date = lists:flatten(io_lib:format("~4..0B-~2..0B-~2..0B ~2..0B-~2..0B-~2..0B", [Y, M, D, Hour, Min, Sec])),
17 Date
18 end || tt<-lists:seq(1,N)],
19 EndTime = timestamp_in_millinsec(),
20 EndTime - StartTime.
21
22 test_my(N) ->
23 StartTime = timestamp_in_millinsec(),
24
25 [begin
26 {{Y,M,D},{H,Mn,S}}=erlang:localtime(),
27 F=fun(X) -> if X>9 -> integer_to_list(X) ; true -> "0"++integer_to_list(X) end end,
28 Ys = F(Y),
29 Ms = F(M),
30 Ds = F(D),
31 Hs = F(H),
32 Mns = F(Mn),
33 Ss = F(S),
34 Date = (Ys ++ "-" ++ Ms ++ "-" ++ Ds ++ " " ++ Hs ++ ":" ++ Mns ++":" ++Ss),
35 Date
36 end || tt<-lists:seq(1,N)],
37 EndTime = timestamp_in_millinsec(),
38 EndTime - StartTime.
42
43 timestamp_in_millinsec()->
44 {MegaSec,Sec,MicoSec}=erlang:now(),
45 MegaSec * 1000000000+Sec*1000+MicoSec div 1000.
46