Created
November 21, 2013 04:49
-
-
Save fsword/7576243 to your computer and use it in GitHub Desktop.
erlang 虚拟机简单测试
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(ipctest). | |
-export([oneway/0, consumer/0, pingpong/0]). | |
oneway() -> | |
N = 10000000, | |
Pid = spawn(ipctest, consumer, []), | |
Start = erlang:now(), | |
dotimes(N - 1, fun () -> Pid ! message end), | |
Pid ! {done, self()}, | |
receive ok -> ok end, | |
Stop = erlang:now(), | |
N / time_diff(Start, Stop). | |
pingpong() -> | |
N = 10000000, | |
Pid = spawn(ipctest, consumer, []), | |
Start = erlang:now(), | |
Message = {ping, self()}, | |
dotimes(N, fun () -> | |
Pid ! Message, | |
receive pong -> ok end | |
end), | |
Stop = erlang:now(), | |
N / time_diff(Start, Stop). | |
consumer() -> | |
receive | |
message -> consumer(); | |
{done, Pid} -> Pid ! ok; | |
{ping, Pid} -> | |
Pid ! pong, | |
consumer() | |
end. | |
%% code omitted - see previous post | |
dotimes(0, _) -> done; | |
dotimes(N, F) -> | |
F(), | |
dotimes(N - 1, F). | |
time_diff({A1,A2,A3}, {B1,B2,B3}) -> | |
(B1 - A1) * 1000000 + (B2 - A2) + (B3 - A3) / 1000000.0 . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(spawntest). | |
-export([serial_spawn/1]). | |
serial_spawn(M) -> | |
N = 1000000, | |
NpM = N div M, | |
Start = erlang:now(), | |
dotimes(M, fun () -> serial_spawn(self(), NpM) end), | |
dotimes(M, fun () -> receive X -> X end end), | |
Stop = erlang:now(), | |
(NpM * M) / time_diff(Start, Stop). | |
serial_spawn(Who, 0) -> Who ! done; | |
serial_spawn(Who, Count) -> | |
spawn(fun () -> | |
serial_spawn(Who, Count - 1) | |
end). | |
dotimes(0, _) -> done; | |
dotimes(N, F) -> | |
F(), | |
dotimes(N - 1, F). | |
time_diff({A1,A2,A3}, {B1,B2,B3}) -> | |
(B1 - A1) * 1000000 + (B2 - A2) + (B3 - A3) / 1000000.0 . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment