直接上代码:
-module (tl). -export ([t1/0, t2/0]). t1() -> [ X || X <- lists:seq(1, 5000) ]. t2() -> t2(lists:seq(1, 5000)). t2([H|T]) -> H, t2(T); t2([]) -> ok.
tc代码:
%% =================================================================== %% Author xiaotie %% 2015-07-30 %% 单进程循环测试:LoopTimes是循环次数 %% tc:t(Module, Function, ArgsList, LoopTimes). %% 多进程并发测试:SpawnProcessesCount是并发的进程数 %% tc:ct(Module, Function, ArgsList, SpawnProcessesCount). %% =================================================================== -module (tc). -export ([t/4, ct/4]). tc(M, F, A) -> {Microsecond, _} = timer:tc (M, F, A), Microsecond. distribution(List, Aver) -> distribution(List, Aver, 0, 0). distribution([H|T], Aver, Greater, Less) -> case H > Aver of true -> distribution(T, Aver, Greater + 1, Less); false -> distribution(T, Aver, Greater, Less + 1) end; distribution([], _Aver, Greater, Less) -> {Greater, Less}. %% =================================================================== %% test: one process test N times %% =================================================================== t(M, F, A, N) -> {Max, Min, Sum, Aver, Greater, Less} = loop ({M, F, A}, N), io:format ("=====================~n"), io:format ("execute [~p] times of {~p, ~p, ~p}:~n", [N, M, F, A]), io:format ("Maximum: ~p(μs)\t~p(s)~n", [Max, Max / 1000000]), io:format ("Minimum: ~p(μs)\t~p(s)~n", [Min, Min / 1000000]), io:format ("Sum: ~p(μs)\t~p(s)~n", [Sum, Sum / 1000000]), io:format ("Average: ~p(μs)\t~p(s)~n", [Aver, Aver / 1000000]), io:format ("Greater: ~p~nLess: ~p~n", [Greater, Less]), io:format ("=====================~n"). loop({M, F, A}, N) -> loop ({M, F, A}, N, 1, 0, 0, 0, []). loop({M, F, A}, N, I, Max, Min, Sum, List) when N >= I -> Microsecond = tc (M, F, A), NewSum = Sum + Microsecond, if Max == 0 -> NewMax = NewMin = Microsecond; Max < Microsecond -> NewMax = Microsecond, NewMin = Min; Min > Microsecond -> NewMax = Max, NewMin = Microsecond; true -> NewMax = Max, NewMin = Min end, loop ({M, F, A}, N, I + 1, NewMax, NewMin, NewSum, [Microsecond|List]); loop({_M, _F, _A}, N, _I, Max, Min, Sum, List) -> Aver = Sum / N, {Greater, Less} = distribution(List, Aver), {Max, Min, Sum, Aver, Greater, Less}. %% =================================================================== %% Concurrency test: N processes each test one time %% =================================================================== ct(M, F, A, N) -> {Max, Min, Sum, Aver, Greater, Less} = cloop ({M, F, A}, N), io:format ("=====================~n"), io:format ("spawn [~p] processes of {~p, ~p, ~p}:~n", [N, M, F, A]), io:format ("Maximum: ~p(μs)\t~p(s)~n", [Max, Max / 1000000]), io:format ("Minimum: ~p(μs)\t~p(s)~n", [Min, Min / 1000000]), io:format ("Sum: ~p(μs)\t~p(s)~n", [Sum, Sum / 1000000]), io:format ("Average: ~p(μs)\t~p(s)~n", [Aver, Aver / 1000000]), io:format ("Greater: ~p~nLess: ~p~n", [Greater, Less]), io:format ("=====================~n"). cloop({M, F, A}, N) -> CollectorPid = self(), ok = loop_spawn({M, F, A}, CollectorPid, N), collector(0, 0, 0, N, 1, []). loop_spawn({M, F, A}, CollectorPid, N) when N > 0 -> spawn_link(fun() -> worker({M, F, A}, CollectorPid) end), loop_spawn({M, F, A}, CollectorPid, N - 1); loop_spawn(_, _, 0) -> ok. collector(Max, Min, Sum, N, I, List) when N >= I -> receive {result, Microsecond} -> NewSum = Sum + Microsecond, if Max == 0 -> NewMax = NewMin = Microsecond; Max < Microsecond -> NewMax = Microsecond, NewMin = Min; Min > Microsecond -> NewMax = Max, NewMin = Microsecond; true -> NewMax = Max, NewMin = Min end, collector(NewMax, NewMin, NewSum, N, I + 1, [Microsecond|List]) after 10000 -> ok end; collector(Max, Min, Sum, N, _, List) -> Aver = Sum / N, {Greater, Less} = distribution(List, Aver), {Max, Min, Sum, Aver, Greater, Less}. worker({M, F, A}, CollectorPid) -> Microsecond = tc(M, F, A), CollectorPid ! {result, Microsecond}.
结果:
==> etoml (compile) ==> simple_im (compile) Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false] Eshell V7.0 (abort with ^G) 1> tc:t(tl, t1, [], 5000). ===================== execute [5000] times of {tl, t1, []}: Maximum: 357(μs) 3.57e-4(s) Minimum: 98(μs) 9.8e-5(s) Sum: 537592(μs) 0.537592(s) Average: 107.5184(μs) 1.075184e-4(s) Greater: 957 Less: 4043 ===================== 2> tc:t(tl, t2, [], 5000). ===================== execute [5000] times of {tl, t2, []}: Maximum: 321(μs) 3.21e-4(s) Minimum: 81(μs) 8.1e-5(s) Sum: 431156(μs) 0.431156(s) Average: 86.2312(μs) 8.62312e-5(s) Greater: 1680 Less: 3320 =====================
相关推荐
rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统。它遵循Mozilla Public License开源协议,采用 Erlang 实现的工业级的消息队列(MQ)服务器,Rabbit MQ 是建立在Erlang OTP平台上。
rabbitmq和erlang,rabbitmq和erlang,rabbitmq和erlang,rabbitmq和erlang,rabbitmq和erlang,rabbitmq和erlang,rabbitmq和erlang,rabbitmq和erlang,rabbitmq和erlang
《erlang编程指南》是对erlang语言的深入介绍。erlang是任何必须并发、容错和快速响应的环境的理想编程...本书的重点集中在解释erlang的语法和语义,并且介绍了其模式匹配、规范列表、递归、调试、网络和并发性等内容。
ErlangB和ErlangC计算工具(exe可执行文件+excel两个) ErlangB和ErlangC计算工具(exe可执行文件+excel两个)
erlang的timer和实现机制 Erlang程序设计
(494页带目录的高清扫描版) 这是一本讲解Erlang编程语言的入门指南,内容通俗...内容涉及模块、函数、类型、递归、错误和异常、常用数据结构、并行编程、多处理、OTP、事件处理,以及所有Erlang的重要特性和强大功能。
erlang整理的一些心得和lunix查看cpu和内存信息的方法
erlang入门电子书 erlang编程 Introducing Erlang,作者Simon.St.Laurent
erlang 安装包
.erl是Erlang编程语言的源文件格式 定义 话务量单位erl定义: 话务量的大小取决于单位时间(1小时)内平均发生的呼叫次数λ和每次呼叫平均占用信道时间S A=S(小时/次)*λ(次/小时) 话务量的单位为爱尔兰...
Erlang及其应用Erlang及其应用Erlang及其应用
Erlang的高级特性和应用Erlang的高级特性和应用
erlang25.0 windows版本
erlang otp25 win安装包
安装Erlang和RabbitMQ,linux系统下的哦,有红色字体标注!已踩坑
erlang22最新下载包 erlang22.1.tar.gz erlang22最新下载包 erlang22最新下载包
esl-erlang_23.0和rabbitmq-3.8.4windows版本 直接下载安装就行,可以直接下载就可安装,非常的方便 ,欢迎大家下载 注意事项: 1. Erlang版本和RabbitMQ版本要配套 (Erlang23.0, RabbitMQ3.8.4) 2. amd芯片请乖乖...
Erlang并发编程,Erlang程序设计,Erlang中文手册。 学习erlang的好资料。 Erlang是一个结构化,动态类型编程语言,内建并行计算支持。最初是由爱立信专门为通信应用设计的,比如控制交换机或者变换协议等,因此...