我目前正在玩最小的网络服务器,像牛仔。我想在URL中传递一个数字,文件的加载行,对这些行进行排序,并在中间打印元素以测试IO和排序。因此,代码像/123一样加载路径,从数字中生成一个填充的"00123“,加载文件"input00123.txt”并对其内容进行排序,然后返回类似于"input00123.txt 0.50000“的内容。
在sime时,我有一个测试工具,它同时发出50个请求,其中只有2个被回答,其余的超时。
我的处理程序如下所示:
-module(toppage_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
readlines(FileName) ->
{ok, Device} = file:open(FileName, [read]),
get_all_lines(Device, []).
get_all_lines(Device, Accum) ->
case io:get_line(Device, "") of
eof -> file:close(Device), Accum;
Line -> get_all_lines(Device, Accum ++ [Line])
end.
handle(Req, State) ->
{PathBin, _} = cowboy_req:path(Req),
case PathBin of
<<"/">> -> Output = <<"Hello, world!">>;
_ -> PathNum = string:substr(binary_to_list(PathBin),2),
Num = string:right(PathNum, 5, $0),
Filename = string:concat("input",string:concat(Num, ".txt")),
Filepath = string:concat("../data/",Filename),
SortedLines = lists:sort(readlines(Filepath)),
MiddleIndex = erlang:trunc(length(SortedLines)/2),
MiddleElement = lists:nth(MiddleIndex, SortedLines),
Output = iolist_to_binary(io_lib:format("~s\t~s",[Filename,MiddleElement]))
end,
{ok, ReqRes} = cowboy_req:reply(200, [], Output, Req),
{ok, ReqRes, State}.
terminate(_Reason, _Req, _State) ->
ok.我在Windows上运行它是为了将它与.NET进行比较。有什么东西可以使它更具有性能呢?比如在线程中运行排序/IO,或者如何改进它?和cygwin一起跑步并没有改变很多结果,我得到了大约5-6个请求的答复。
提前感谢!
发布于 2013-08-25 10:00:37
最突出的问题是:get_all_lines是O(N^2),因为列表连接(++)是O(N)。Erlang列表类型是一个单链接列表。这里的典型方法是使用"cons“操作符,将其附加到列表的头上,并在末尾使用反向累加器:
get_all_lines(Device, Accum) ->
case io:get_line(Device, "") of
eof -> file:close(Device), lists:reverse(Accum);
Line -> get_all_lines(Device, [Line | Accum])
end.将binary标志传递给file:open以使用二进制文件而不是字符串(这些字符串只是Erlang中的字符列表),它们更适合内存和CPU。
https://stackoverflow.com/questions/18399966
复制相似问题