테스트:두 가지 구조 문자열 방식 의 성능 비교

먼저 두 편의 문장 을 추천 합 니 다.
http://www.wagerlabs.com/blog/2008/02/parsing-text-an.html
http://ppolv.wordpress.com/2008/02/25/parsing-csv-in-erlang/
(모두 담 을 넘 어 방문 해 야 한다.빌어먹을'쿵 푸 망')
Erlang 에서 텍스트 프로 토 콜 을 해석 하고 Binary 를 사용 하 는 것 은 효율 적 인 선택 임 에 틀림없다.그러나 나 는 글 에서 Binary 의 각 바이트 에 대해 문자열 로 조합 하 는 것 은 모두 list 를 사용 하 는 것 을 발견 했다.
NewList = lists:reverse([Char|OldList])
...이 아니 라
NewList = binary_to_list(<>)
잠시 후에 저 는 테스트 를 해서 대량의 짧 은 문자열 의 구성 을 증 명 했 습 니 다.예 를 들 어'GET/index.html HTTP/1.1'>을'GET','/index.html','HTTP/1.1'로 해석 하면 list 를 사용 하 는 것 이 좋 습 니 다.
간단하게 순환 테스트 코드 를 썼 습 니 다:

test_append() -> 
    test_char_append(100),
    test_char_append(1000),
    test_char_append(10000),
    test_char_append(100000),
    test_char_append(1000000),
    test_char_append(10000000),
    test_field_append(10000),
    test_field_append(100000),
    test_field_append(200000),
    test_field_append(300000).

test_char_append(Loop) ->
    erlang:statistics(wall_clock),
    test_char_append_by_list(Loop, []),
    {_,T1} = erlang:statistics(wall_clock),
    test_char_append_by_binary(Loop, <<>>),
    {_,T2} = erlang:statistics(wall_clock),
    io:format("~p loops, test_char_append_by_list using time: ~pms~n", [Loop,T1]),
    io:format("~p loops, test_char_append_by_binary using time: ~pms~n~n", [Loop,T2]),
    ok.

test_field_append(Loop) ->
    erlang:statistics(wall_clock),
    test_field_append_by_list(Loop, []),
    {_,T1} = erlang:statistics(wall_clock),
    test_field_append_by_binary(Loop, []),
    {_,T2} = erlang:statistics(wall_clock),
    io:format("~p loops, test_field_append_by_list using time: ~pms~n", [Loop,T1]),
    io:format("~p loops, test_field_append_by_binary using time: ~pms~n~n", [Loop,T2]),
    ok.
    
test_char_append_by_list(0, List) -> lists:reverse(List);
test_char_append_by_list(N, List) -> test_char_append_by_list(N-1, [$!|List]).

test_char_append_by_binary(0, Bin) -> binary_to_list(Bin);
test_char_append_by_binary(N, Bin) -> test_char_append_by_binary(N-1, <<Bin/binary, $!>>).

test_field_append_by_list(0, List) -> lists:reverse(List);
test_field_append_by_list(N, List) -> 
    Field = test_char_append_by_list(100, []),
    test_field_append_by_list(N-1, [Field|List]).

test_field_append_by_binary(0, List) -> lists:reverse(List);
test_field_append_by_binary(N, List) -> 
    Field = test_char_append_by_binary(100, <<>>),
    test_field_append_by_binary(N-1, [Field|List]).

출력 은 다음 과 같 습 니 다:
인용 하 다.
100 loops, test_char_append_by_list using time: 0ms
100 loops, test_char_append_by_binary using time: 0ms
1000 loops, test_char_append_by_list using time: 0ms
1000 loops, test_char_append_by_binary using time: 0ms
10000 loops, test_char_append_by_list using time: 0ms
10000 loops, test_char_append_by_binary using time: 0ms
100000 loops, test_char_append_by_list using time: 16ms
100000 loops, test_char_append_by_binary using time: 16ms
1000000 loops, test_char_append_by_list using time: 203ms
1000000 loops, test_char_append_by_binary using time: 156ms
10000000 loops, test_char_append_by_list using time: 2922ms
10000000 loops, test_char_append_by_binary using time: 1594ms
10000 loops, test_field_append_by_list using time: 62ms
10000 loops, test_field_append_by_binary using time: 172ms
100000 loops, test_field_append_by_list using time: 1109ms
100000 loops, test_field_append_by_binary using time: 1860ms
200000 loops, test_field_append_by_list using time: 2672ms
200000 loops, test_field_append_by_binary using time: 4937ms
300000 loops, test_field_append_by_list using time: 3438ms
300000 loops, test_field_append_by_binary using time: 7062ms
이 를 통 해 알 수 있 듯 이 문자열 이 짧 을 때 list 를 사용 하 는 것 이 binary 보다 속도 가 더 좋 습 니 다.문자열 이 10w 이상 이면(누가 그렇게 긴 list 를 할 일이 없 습 니까?)바 이 너 리 가 조금 우세 한 거 야.짧 은 문자열 을 대량으로 구성 할 때 는 list 로 조합 하고 반전 하 는 것 이 좋 습 니 다.

좋은 웹페이지 즐겨찾기