I wrote the following two examples to test the concurrency of Zig and Erlang.
% Erlang
-module(spwntest).
disp()->
_ = 234.
main(_) ->
F = fun(_) -> spawn(fun() -> disp() end) end,
lists:foreach(F,lists:seq(1,2000000)),
io:format("done").
%% Result: All Erlang processes complete their work in less than 10 seconds
//!Zig
const std = @import("std");
pub fn main() !void{
for(0..100000) |_|{
const t = try std.Thread.spawn(.{},disp,.{});
t.join();
}
std.debug.print("done",.{});
}
fn disp() void{
_ = 10;
}
Result: Zig takes a lot of time for all threads finish their work.
According to my understanding, Erlang processes are more efficient and help develop scalable applications when compared with Zig concurrency.
I want to know from the opinions of experts to help my understanding.
There is no concurrency in your Zig code. You call
joinafter creating each and every thread. This makes your code sequential. Also,std.Thread.spawncreates an OS thread. Whereas in Erlangspawncreates something else. From Wikipedia:Here's the corrected code: