小测试:两种构造字符串方式的性能对比

先推荐两篇文章:
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(<<OldBin/binary,$Char>>)

稍后我做了个测试,证明了对于大量短字符串的构成,比如将 <<"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]).


输出大致如下:

[quote]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[/quote]

可见当字符串较短时,使用list比binary速度更佳,当字符串达到10w以上(谁没事搞那么长的list?),binary才有一点点的优势。在大量构造短字符串时,还是乖乖用list组合并反转吧
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值