<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1187.4">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 12.0px Helvetica}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 12.0px Helvetica; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 12.0px; font: 16.0px Helvetica; color: #011892}
p.p4 {margin: 0.0px 0.0px 0.0px 12.0px; font: 16.0px Helvetica; color: #011892; min-height: 19.0px}
p.p5 {margin: 0.0px 0.0px 0.0px 12.0px; font: 12.0px Helvetica; color: #011892}
p.p6 {margin: 0.0px 0.0px 0.0px 12.0px; font: 12.0px Helvetica; color: #011892; min-height: 14.0px}
p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 12.0px Helvetica; min-height: 14.0px}
span.s1 {direction: ltr; unicode-bidi: embed}
span.s2 {text-decoration: underline ; direction: ltr; unicode-bidi: embed}
span.s3 {text-decoration: underline}
</style>
</head>
<body>
<p class="p1">It looks like you run these benchmarks in the shell.</p>
<p class="p2"><br></p>
<p class="p1">Code run in the shell is interpreted in Erlang and much slower than compiled code.</p>
<p class="p2"><br></p>
<p class="p1">Besides looping with a comprehension and building a large list might also more overhead than necessary.</p>
<p class="p2"><br></p>
<p class="p1">On 2013-12-22 12:29:43 +0000, Alexander Petrovsky said:</p>
<p class="p2"><br></p>
<p class="p3">Hi!</p>
<p class="p4"><br></p>
<p class="p3">I perform the same test on my machine:</p>
<p class="p4"><br></p>
<p class="p3">For Ruby:</p>
<p class="p4"><br></p>
<p class="p3"># irb</p>
<p class="p4"><br></p>
<p class="p5">require 'benchmark'</p>
<p class="p6"><br></p>
<p class="p5">n = 50000</p>
<p class="p6"><br></p>
<p class="p5">text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
<p class="p5">=> "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
<p class="p6"><br></p>
<p class="p5">puts Benchmark.measure { n.times { text.split /\s+/ } }</p>
<p class="p4"><br></p>
<p class="p3">3.850000 0.010000 3.860000 ( 3.955543)</p>
<p class="p4"><br></p>
<p class="p3">For Erlang:</p>
<p class="p4"><br></p>
<p class="p3"># erl</p>
<p class="p4"><br></p>
<p class="p5">N = lists:seq(1, 50000).</p>
<p class="p6"><br></p>
<p class="p5">Text = <<"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.">>.</p>
<p class="p6"><br></p>
<p class="p5">F = fun() -> {ok, Pattern} = re:compile("\\s+"), re:split(Text, Pattern), ok end.</p>
<p class="p6"><br></p>
<p class="p5">F1 = fun() -> [F() || _ <- N] end.</p>
<p class="p6"><br></p>
<p class="p5">{T, _} = timer:tc(F1).</p>
<p class="p6"><br></p>
<p class="p5">T / 1000000.</p>
<p class="p4"><br></p>
<p class="p3">13.7556</p>
<p class="p4"><br></p>
<p class="p3"># erl</p>
<p class="p4"><br></p>
<p class="p5">N = lists:seq(1, 50000).</p>
<p class="p6"><br></p>
<p class="p5">Text = <<"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.">>.</p>
<p class="p6"><br></p>
<p class="p5">{ok, Pattern} = re:compile("\\s+").</p>
<p class="p6"><br></p>
<p class="p5">F = fun() -> re:split(Text, Pattern), ok end.</p>
<p class="p6"><br></p>
<p class="p5">F1 = fun() -> [F() || _ <- N] end.</p>
<p class="p6"><br></p>
<p class="p5">{T, _} = timer:tc(F1).</p>
<p class="p6"><br></p>
<p class="p5">T / 1000000.</p>
<p class="p6"><br></p>
<p class="p3">12.8033</p>
<p class="p4"><br></p>
<p class="p3"># erl -smp disable</p>
<p class="p4"><br></p>
<p class="p5">N = lists:seq(1, 50000).</p>
<p class="p6"><br></p>
<p class="p5">Text = <<"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.">>.</p>
<p class="p6"><br></p>
<p class="p5">F = fun() -> {ok, Pattern} = re:compile("\\s+"), re:split(Text, Pattern), ok end.</p>
<p class="p6"><br></p>
<p class="p5">F1 = fun() -> [F() || _ <- N] end.</p>
<p class="p6"><br></p>
<p class="p5">{T, _} = timer:tc(F1).</p>
<p class="p6"><br></p>
<p class="p5">T / 1000000.</p>
<p class="p4"><br></p>
<p class="p3">8.927621</p>
<p class="p4"><br></p>
<p class="p3"># erl -smp disable</p>
<p class="p4"><br></p>
<p class="p5">N = lists:seq(1, 50000).</p>
<p class="p6"><br></p>
<p class="p5">Text = <<"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.">>.</p>
<p class="p6"><br></p>
<p class="p5">{ok, Pattern} = re:compile("\\s+").</p>
<p class="p6"><br></p>
<p class="p5">F = fun() -> re:split(Text, Pattern), ok end.</p>
<p class="p6"><br></p>
<p class="p5">F1 = fun() -> [F() || _ <- N] end.</p>
<p class="p6"><br></p>
<p class="p5">{T, _} = timer:tc(F1).</p>
<p class="p6"><br></p>
<p class="p5">T / 1000000.</p>
<p class="p4"><br></p>
<p class="p3">8.657157</p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3">The ruby and erlang utilize my cpu for 100%. As you can see, I make some tricks, it make erlang a little bit faster, but it still not enough.</p>
<p class="p4"><br></p>
<p class="p3">If you need process lagre amount of text, you should use erlang:spawn for each text object, it will be very fast.</p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3">2013/12/18 akonsu <span class="s1"><<a href="mailto:akonsu@gmail.com"><span class="s2">akonsu@gmail.com</span></a>></span></p>
<p class="p3">I have two benchmarks that perform a simple text split on a regular expression. One is in Ruby and another is in Erlang. The Erlang version is 6 times slower on my machine for some reason. I have read all documentation I could find on how to use binaries in Erlang, but I cannot make it faster. I am looking for help.</p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3">Here is the code:</p>
<p class="p4"><br></p>
<p class="p3">Ruby:</p>
<p class="p4"><br></p>
<p class="p3">require 'benchmark'</p>
<p class="p4"><br></p>
<p class="p3">n = 50000</p>
<p class="p3">text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
<p class="p4"><br></p>
<p class="p3">puts Benchmark.measure {</p>
<p class="p3"> n.times { text.split /\s+/ }</p>
<p class="p3">}</p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3">Erlang:</p>
<p class="p4"><br></p>
<p class="p3">text() -></p>
<p class="p3"> <<"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.">>.</p>
<p class="p4"><br></p>
<p class="p3">times(0, _) -></p>
<p class="p3"> ok;</p>
<p class="p3">times(N, F) -></p>
<p class="p3"> F(),</p>
<p class="p3"> times(N - 1, F).</p>
<p class="p4"><br></p>
<p class="p3">measure(N) -></p>
<p class="p3"> {ok, Pattern} = re:compile("\\s+"),</p>
<p class="p3"> B = text(),</p>
<p class="p3"> F = fun() -> re:split(B, Pattern) end,</p>
<p class="p3"> {T, ok} = timer:tc(?MODULE, times, [N, F]),</p>
<p class="p3"> T / 1000000.</p>
<p class="p4"><br></p>
<p class="p3">Ruby outputs</p>
<p class="p4"><br></p>
<p class="p3"> 3.180000 0.000000 3.180000 ( 3.182452)</p>
<p class="p4"><br></p>
<p class="p3">Erlang outputs</p>
<p class="p4"><br></p>
<p class="p3">Erlang R16B03 (erts-5.10.4) [source] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]</p>
<p class="p4"><br></p>
<p class="p3">Eshell V5.10.4 (abort with ^G)</p>
<p class="p3">1> test:measure(50000).</p>
<p class="p3">18.261952</p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3">_______________________________________________</p>
<p class="p3">erlang-questions mailing list</p>
<p class="p3"><span class="s3"><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a></span></p>
<p class="p3"><span class="s3"><a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a></span></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p4"><br></p>
<p class="p3">--<span class="Apple-converted-space"> </span></p>
<p class="p3">Петровский Александр / Alexander Petrovsky,</p>
<p class="p4"><br></p>
<p class="p3">Skype: askjuise</p>
<p class="p3">Jabber: <a href="mailto:juise@jabber.ru"><span class="s3">juise@jabber.ru</span></a></p>
<p class="p3">Phone: +7 914 8 820 815 (irkutsk)</p>
<p class="p7"><br></p>
</body>
</html>