How fast can Erlang create processes?

本文测试了Erlang在不同配置下创建和销毁进程的速度,发现BEAM虚拟机每秒能创建2百万个进程,平均每个进程仅需0.5微秒。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址: [url]http://www.lshift.net/blog/2006/09/10/how-fast-can-erlang-create-processes[/url]

Very fast indeed.

1> spawntest:serial_spawn(1).
3.58599e+5

That’s telling me that Erlang can create and tear down processes at a rate of roughly 350,000 Hz. The numbers change slightly - things slow down - if I’m running the test in parallel:

2> spawntest:serial_spawn(10).
3.48489e+5
3> spawntest:serial_spawn(10).
3.40288e+5

4> spawntest:serial_spawn(100).
3.35983e+5
5> spawntest:serial_spawn(100).
3.36743e+5

[Update: I forgot to mention earlier that the system seems to spend 50% CPU in user and 50% in system time. Very odd! I wonder what the Erlang runtime is doing to spend so much system time?]

Here’s the code for what I’m doing:

-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 .

This is all on an Intel Pentium 4 running at 2.8GHz, with 1MB cache, on Debian linux, with erlang_11.b.0-3_all.deb.

我的实验如下:

root@nd-desktop:~/otp_src_R13B01# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 23
model name : Pentium(R) Dual-Core CPU E5200 @ 2.50GHz
stepping : 6
cpu MHz : 1200.000
cache size : 2048 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 10
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm lahf_lm
bogomips : 4988.06
clflush size : 64
power management:

^Croot@nd-desktop:~# erl -smp disable
Erlang R13B01 (erts-5.7.2) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.2 (abort with ^G)
1> os:getpid().
"14244"
2> spawntest:serial_spawn(1).
749026.6398814741


^Croot@nd-desktop:~/otp_src_R13B01# erl -pa /root
Erlang R13B01 (erts-5.7.2) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.2 (abort with ^G)
1> spawntest:serial_spawn(1).
402022.00990099803

结论是 beam比beam.smp要快很多,因为是所以的锁都去掉了。
我们来strace -o beam.trace.out -p 14244
root@nd-desktop:~# tail beam.trace.txt
clock_gettime(CLOCK_MONOTONIC, {3240006, 740886937}) = 0
clock_gettime(CLOCK_MONOTONIC, {3240006, 740914525}) = 0
clock_gettime(CLOCK_MONOTONIC, {3240006, 740942182}) = 0
clock_gettime(CLOCK_MONOTONIC, {3240006, 740970048}) = 0
clock_gettime(CLOCK_MONOTONIC, {3240006, 740997775}) = 0
clock_gettime(CLOCK_MONOTONIC, {3240006, 741025363}) = 0
clock_gettime(CLOCK_MONOTONIC, {3240006, 741053090}) = 0
clock_gettime(CLOCK_MONOTONIC, {3240006, 741080956}) = 0
clock_gettime(CLOCK_MONOTONIC, {3240006, 741109242}) = 0
clock_gettime(CLOCK_MONOTONIC, {3240006, 741137318}) = 0
发现大量的sys调用, user和sys的时间都很多,这是不正常的。

gdb看了下 原来是
erl_process.c:alloc_process()
{...
erts_get_emu_time(&p->started); /* 获取进程创建时间*/
...
}

昂贵的系统调用哦 去掉它。。。
重新编译再看下结果。

root@nd-desktop:~/otp_src_R13B01# bin/erl -smp disable -pa /root
Erlang R13B01 (erts-5.7.2) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.2 (abort with ^G)
1> spawntest:serial_spawn(1).
2264041.5859158505
2>

root@nd-desktop:~/otp_src_R13B01# bin/erl -pa /root
Erlang R13B01 (erts-5.7.2) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.2 (abort with ^G)
1> spawntest:serial_spawn(1).
652946.9454488944
2>

看到了吧 在beam vm下每秒最多可创建 2百万个进程 比原来快了3倍, 也就是说 创建一个进程并且销毁的开心是 [color=red]0.5us[/color] 太快了, 我的机器的bogo mips是4988, 哈哈 2500个指令就搞定了 快快快。。。

结论是: 系统调用对于服务器的性能是很大的杀手!!!
内容概要:该论文聚焦于T2WI核磁共振图像超分辨率问题,提出了一种利用T1WI模态作为辅助信息的跨模态解决方案。其主要贡献包括:提出基于高频信息约束的网络框架,通过主干特征提取分支和高频结构先验建模分支结合Transformer模块和注意力机制有效重建高频细节;设计渐进式特征匹配融合框架,采用多阶段相似特征匹配算法提高匹配鲁棒性;引入模型量化技术降低推理资源需求。实验结果表明,该方法不仅提高了超分辨率性能,还保持了图像质量。 适合人群:从事医学图像处理、计算机视觉领域的研究人员和工程师,尤其是对核磁共振图像超分辨率感兴趣的学者和技术开发者。 使用场景及目标:①适用于需要提升T2WI核磁共振图像分辨率的应用场景;②目标是通过跨模态信息融合提高图像质量,解决传统单模态方法难以克服的高频细节丢失问题;③为临床诊断提供更高质量的影像资料,帮助医生更准确地识别病灶。 其他说明:论文不仅提供了详细的网络架构设计与实现代码,还深入探讨了跨模态噪声的本质、高频信息约束的实现方式以及渐进式特征匹配的具体过程。此外,作者还对模型进行了量化处理,使得该方法可以在资源受限环境下高效运行。阅读时应重点关注论文中提到的技术创新点及其背后的原理,理解如何通过跨模态信息融合提升图像重建效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值