我在Eclipse中运行了下面的代码。编译代码,并在Eclipse工作区的scr
文件夹中创建.txt
文件,并将数据的头传递给第一个io:format
,但second io:format
抛出错误参数,我不知道如何解决它。
%% @author PC
%% @doc @todo Add description to pingping.
-module(pingping).
-export([run/2, run/3]).
-include("conf.hrl").
run(DataSize, R) ->
OutFileLocation = "out_cerl_pingping.txt",
case file:open(OutFileLocation, [append]) of
{error, Why} ->
?ERR_REPORT("L'archivio non esiste!", Why);
{ok, OutFile} ->
run(DataSize, R, OutFile)
end.
run(DataSize, R, OutFile) ->
Data = generate_data(DataSize),
Self = self(),
SpawnStart = time_microseg(),
P1 = spawn(fun() -> pingping(Data, Self, R) end),
P2 = spawn(fun() -> pingping(Data, Self, R) end),
SpawnEnd = time_microseg(),
TimeStart = time_microseg(),
P1 ! {init, self(), P2},
P2 ! {init, self(), P1},
finalize(P1),
finalize(P2),
TimeEnd = time_microseg(),
TotalTime = TimeEnd - TimeStart,
SpawnTime = SpawnEnd - SpawnStart,
printResult(Data, R, TotalTime, SpawnTime, OutFile).
pingping(_, Parent, 0) ->
Parent ! {finish, self()};
pingping(Data, Parent, R) ->
receive
{init, Parent, Peer} ->
Peer ! {self(), Data},
pingping(Data, Parent, R-1);
{Peer, _} ->
Peer ! {self(), Data},
pingping(Data, Parent, R-1)
end.
finalize(Pid) ->
receive
{finish, Pid} -> ok
end.
printResult(Data, R, Time_exec, Time_spawn, OutFile) ->
FormatH = "~-9s\t ~-13s\t ~-22s\t ~-11s\t ~-10s~n",
Header = ["#bytes", "#repetitions", "exec_time[microsec]", "MBytes/sec", "spawn_time"],
io:format(OutFile, FormatH, Header),
MBps = bandwidth_calc(Data, Time_exec),
FormatD = "~-9w\t ~-13w\t ~-22f\t ~-11f\t ~-3f~n",
io:format(OutFile,FormatD, [size(Data), R, Time_exec, MBps, Time_spawn ]).
bandwidth_calc(Data, Time) ->
Megabytes = (size(Data) / math:pow(2, 20)),
Seconds = (Time * 1.0e-6),
Megabytes / Seconds.
generate_data(Size) -> generate_data(Size, []).
generate_data(0, Bytes) ->
list_to_binary(Bytes);
generate_data(Size, Bytes) ->
generate_data(Size - 1, [1 | Bytes]).
time_microseg() ->
{MS, S, US} = now(),
(MS * trunc(1.0e+12)) + (S * trunc(1.0e+6)) + US.
%-define(OUT_FILE, "erl_out.txt").
-define(ERR_REPORT(Msg, Reason), io:format("%%%%%%%%%%%%%% ERROR %%%%%%%%%%%%%%\n" ++
"Msg: ~s\n" ++
"Reason: ~s\n\n", [Msg, Reason])).
2条答案
按热度按时间eagi6jfj1#
您的代码无法编译,因为
conf.hrl
包含文件没有显示,所以我猜它提供了底部提供的ERR_REPORT
宏。您没有包含如何运行代码的说明,所以我猜测并调用了pingping:run(3, 3).
,这导致了Erlang/OTP 26.1下的错误:错误消息给了我们答案:你想把整数打印成浮点数。更改此行:
为了纠正要素3和要素5的格式,
导致
pingping:run(3, 3)
返回ok
和包含以下内容的out_cerl_pingping.txt
文件:bz4sfanl2#
调试的一种方法是将所有格式说明符设置为
~p
,因为它将接受所有Erlang术语,因此至少不会再有badarg
错误,同时我们可以看到打印的实际值。从那里你可以开始改变5个参数中的每一个,以找出哪些参数的数据类型不匹配。
最终答案已经由@steve提供