1. 程式人生 > >系統技術非業餘研究 » leex文法分析的效率

系統技術非業餘研究 » leex文法分析的效率

R13B新新增的leex相當於c的lex, 在做文法分析非常方便,但是效率如何呢? leex的example裡面帶了個erlang_scan和erlang標準的釋出版的erl_scan相容,所以我們來對比測試下效率。

注意用R13B03,因為R13B02的erlc漏掉了編譯xrl格式。

以下是實驗:

[email protected]:~# git clone git://github.com/rvirding/leex.git
[email protected]:~# cd leex/examples/
[email protected]:~/leex/examples# cat test_scan.erl
-module(test_scan).
-export([start/1]).
start([A])->
    {ok, F} = file:open(atom_to_list(?MODULE)++".erl", [read]),
    {ok, S} = file:read(F, 9999999),
    file:close(F),
    N = list_to_integer(A),
    test(N, fun erlang_scan:string/1, S, "erlang_scan"),
    test(N, fun erl_scan:string/1, S, "erl_scan"),
    ok.

test(N, F, S, Ts)->
    Start = erlang:now(),    
    dotimes(N, fun (_) ->
                       F(S)
               end),
    io:format("~s run ~w ms~n", [Ts,round(timer:now_diff(now(), Start) /1000)]).

dotimes(0, _) -> done;
dotimes(N, F) ->
    F(N),
    dotimes(N - 1, F).
[email protected]:~/leex/examples# erlc erlang_scan.xrl
[email protected]:~/leex/examples# ls *.erl
erlang_scan.erl  test_scan.erl
[email protected]:~/leex/examples# erlc *.erl
[email protected]:~/leex/examples# erl -noshell -run test_scan start 10000 -s erlang halt
erlang_scan run 2208 ms
erl_scan run 1181 ms

%% 這個版本稍微慢點

[email protected]:~/leex/examples# erlc +native *.erl
[email protected]:~/leex/examples# erl -noshell -run test_scan start 10000 -s erlang halt
erlang_scan run 1292 ms
erl_scan run 1238 ms

結論是: leex產生的程式碼和手寫的效率幾乎差不多。

Post Footer automatically generated by wp-posturl plugin for wordpress.