테스트:두 가지 구조 문자열 방식 의 성능 비교
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 로 조합 하고 반전 하 는 것 이 좋 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다른 사람의 웹사이트 편집: contenteditable 및 designMode그래도 우리가 그렇게 할 수 있다고 생각하는 것은 멋진 일입니다. 제가 강조하고 싶었던 일종의 관련 API가 실제로 몇 개 있기 때문에 오늘 그것을 가져왔습니다. contenteditable는 "true" 값이 할당...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.