Wednesday, May 15, 2013

Parallel integer summation challenge Erlang vs CLR/C#

Today we have done an interesting benchmark in C# and Erlang. Basically the point was to compare the brevity and performance of two inherently different langs/approaches:

Erlang r16b:

$ cat t.erl

-module(t).
-export([t/0, t/1, ct/1, ct/2]).

-define(DEF_CNT, 10000000).

t() ->
    t(?DEF_CNT).
t(N) when is_integer(N) ->
    time_it(fun() -> test(N) end, N).

ct(Threads) ->
    ct(?DEF_CNT, Threads).
ct(N, Threads) when is_integer(N), is_integer(Threads) ->
    time_it(fun() -> ctest(N, Threads) end, N * Threads).

time_it(Fun, Divisor) ->
    {T, Result} = timer:tc(Fun),
    {T / (Divisor/1000000) / 1000, Result}.

ctest(N, M) ->
    Pids = spawner(self(), N, M),
    lists:foldl(fun(Pid, S) ->
        receive {Pid, Sum} -> S+Sum end
    end, 0, Pids).

spawner(_, _, 0) ->
    [];
spawner(Owner, N, M) ->
    [spawn(fun() -> loop(Owner, N) end) | spawner(Owner, N, M-1)].

loop(Pid, N) ->
    Sum = test(N),
    Pid ! {self(), Sum}.

test(N) -> test(N, 0).

test(0, M) -> M;
test(N, M) -> test(N-1, M+1).


C#, NET 4:

private void button4_Click(object sender, EventArgs e)
{
    const int CNT = 100000000;
    const int SPLIT = 1000000;

    var tasks = new Task[CNT / SPLIT];
       
         

    var w = Stopwatch.StartNew();
          
    for(int i=0,start=0; i    {
    tasks[i] = new Task( () =>
                                {
                                    long lsum = 0;
                                    long end = start + SPLIT;
                                    for(int c=start; c                                    return lsum;
                                }
                            );
    tasks[i].Start();
    }

    long sum = tasks.Sum( t => t.Result);
         
    w.Stop();

    var rate = CNT / (w.Elapsed.TotalSeconds);

    Text = string.Format("Total: {0} in {1} ms  at {2}/sec  {3} msec/million ",

  CNT,
 w.Elapsed.TotalMilliseconds, 
rate,
w.Elapsed.TotalMilliseconds / (CNT / 1000000)
);

    var s = sum;
    Text+= s.ToString();
}



Test results are really interesting:
  .NET      0.22 msec per million
  Erlang does this is 1.5 msec per million

CLR is 6.8 faster for this test.

Some other facts.  Linear loop single threaded that sums integers (not optimized out of code, I confirmed in disassembler)

PHP code:
 $CNT = 10000000;
 $sum = 0;
 $w = microtime(true);
 for($i=0; $i<$CNT; $i++) $sum += 1;
 $w = round((microtime(true)-$w)*1000,0);
 echo($w);


PHP standard runtime:  1040 ms
C#:                                     2ms

CLR is 500 times faster that PHP for this simple test




25 comments:

Anonymous said...

whats funny is that Task class really nullifies Erlangs huge benefit of green processes adventage over "heavy" langs like c#

Anonymous said...

Dima, I know that PHP sucks but what other choice now when 90% of my code is in it

dody Love said...

شركة الصفرات لعزل الاسطح بالرياض
شركة الصفرات لعزل الخزانات بالرياض
شركة الصفرات لتنظيف الفلل بالرياض
شركة الصفرات لتنظيف المنازل بالرياض
شركة الصفرات لرش المبيدات بالرياض
شركة الصفرات لمكافحة الحشرات بالرياض
شركة الصفرات لكشف تسربات المياه بالرياض
شركة الصفرات للتنظيف بالرياض
شركة الصفرات لتنظيف الشقق بالرياض
شركة الصفرات لنقل عفش بالرياض
شركة الصفرات لنقل الاثاث بالرياض
شركة الصفرات لخدمات التنظيف

dody Love said...

شركة تسليك المجارى بالرياض
شركة شفط البيارات بالرياض
شركة تنظيف الخزانات بالرياض
شركة عزل الاسطح بالرياض
شركة تنظيف الشقق بالرياض
شركة تنظيف الفلل بالرياض
شركة رش المبيدات بالرياض
شركة مكافحة النمل الابيض بالرياض
شركة غسيل السجاد بالرياض
شركة كشف تسربات المياه بالرياض
شركة نقل الاثاث بالرياض
شركة رش مبيدات بأبها

Unknown said...

ugg boots
air jordan 13
air jordan shoes
beats headphones
bottega veneta outlet
burberry outlet
canada goose outlet
chanel outlet
cheap wedding dresses
coach outlet
dansko outlet
evening dresses
hermes belt
hermes birkin
hermes outlet
juicy couture outlet
kate spade outlet
mbt shoes
michael kors outlet
mizuno running
moncler coats
mulberry outlet
soccer shoes
nike outlet store
the north face outlet
oakley sunglasses
ray ban sunglasses
stuart weitzman boots
surpa sneakers
north face outlet
the north face clearance
the north face jackets
the north face outlet store
north face jackets
the north face uk
tommy hilfiger outlet
tory burch outlet
true religion jeans
vans shoes
ysl outlet

Unknown said...

20150623meiqing
louis vuitton
louis vuitton
fitflop footwear
oakley store
kate spade outlet
ralph lauren outlet
ray ban sunglasses
jordan 8 playoffs
oakley vault
toms outlet
www.louisvuitton.com
hollister
jordan concord 11s
michael kors outlet online
chanel bags
christian louboutin outlet
coach outlet
fitflops clearance
louboutin
louis vuitton outlet
coach outlet
coach outlet store online
marc jacobs handbags
toms promo code
oakley sunglasses wholesale
lebron james basketball shoes
prada
jeremy scott shoes
oakley sunglasses
abercrombie outlet
louis vuitton
discount oakley sunglasses
christian louboutin outlet
michael kors uk
ray ban wayfarer
concords 11
kd shoes
kate spade handbags
pandora charms
abercrombie & fitch

mmjiaxin said...

marc jacobs
nike foamposite
vans shoes
calvin klein outlet
ralph lauren outlet
michael kors outlet online
swarovski crystal
ferragamo outlet
cheap nike shoes
michael kors handbags sale
oakley,occhiali oakley,oakley italia,oakley occhiali,oakley sunglasses
moncler outlet store
the north face outlet
michael kors factory online
ralph lauren uk
nike air max
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
longchamp outlet
michael kors wallet sale
michael kors outlet
mm1202

Unknown said...




شركة الصفا والمروه لخدمات التنظيف
شركة تنظيف بالدمام
شركة تسليك مجاري بالدمام
شركة مكافحة حشرات بالدمام
شركة تنظيف شقق بالدمام
شركة تنظيف فلل بالدمام
شركة تسليك مجاري بالدمام
شركة تنظيف بالخبر
شركة كشف تسربات المياة بالدمام
شركة مكافحة النمل الابيض بالخبر

شركة تسليك مجاري بالجبيل
شركة تسليك مجاري بالخبر
شركة تسليك مجاري بالاحساء
شركة تسليك مجاري بالقطيف
شركة تسليك مجاري بالرياض
شركة تسليك مجاري بجدة
شركة تسليك مجاري بالباحة
شركة تسليك مجاري بتبوك

شركة كشف تسربات المياة بالاحساء
شركة كشف تسربات المياة بالقطيف
شركة كشف تسربات المياة بالجبيل
شركة كشف تسربات المياة بالدمام
شركة تسليك مجاري بالجبيل
شركة كشف تسربات المياة بالخبر

Ann Lily said...

Thanks for giving me the useful information. I think I need it!
See more at: happy wheels | happy wheels game | open facebook | happywheels | happywheels game
I would like more information about this, because it is very nice. Thanks for sharing!

Unknown said...

our website is a professional , dashing and efficient way to communicate with your customers and Professional SMS Marketing are using the Bloomtools SMS Marketing tool will save your

Anonymous said...

To be honest, when we started the project I wasn’t sure we could pull this off but you certainly did it and did it well.

GGG | moto x3m 3 | limaxio | gold miner game | happy wheels games

Unblocked Games 66 said...

Easy smile who is also easy to cry. And at least they'll crushing silence.
Xbox One | Unblocked Games | Friv 2 | Unblocked | Yepi 2 | Titanfall 1

Unknown said...

jaguars jersey
cheap jerseys
oakley sunglasses wholesale
ray ban sunglasses
ugg boots
replica watches
seahawks jersey
ralph lauren outlet
michael kors handbags
ralph lauren

alicetaylor said...

Thanks for sharing this quality information with us. I really enjoyed reading.

lennyfacetext.com

Syifa Zahra said...

thanks obat epilepsi di apotik jenis kista ovarium yang berbahaya

Unknown said...

0728jejeLa méthode chaussures new balance homme prix consiste à joindre la formule Phylon avec la mousse new balance soldes asos lunaire. Le pommeau était fixé à air jordan 1 retro alpha sa poignée en bronze moulé avec new balance femme pas cher sarenza une noix qui avait un point important new balance mode de vie qui, s'il était utilisé avec une tous les air jordan force suffisante, pourrait fracturer le crâne d'un homme. asics gel lyte iii homme pas cher

ركن كلين said...

Thank you very much for accepting the comment
شركة تنظيف فلل بالرياض
ارخص شركة تنظيف منازل بالرياض
شركة تنظيف وغسيل كنب بالرياض
شركة تنظيف السجاد بالرياض
شركة تنظيف ستائربالرياض
شركة تنظيف مكيفات بالرياض
شركة غسيل مكيفات بالرياض
افضل شركة تنظيف مكيفات بالرياض
شركة تنظيف مكيفات اسبلت بالرياض
افضل شركة غسيل مكيفات اسبلت بالرياض
شركة كشف تسربات المياه بالرياض
افضل شركة كشف تسربات المياه بالرياض

cara menggugurkan kandungan secara tradisional said...

just information we only provide information for those who need it

Tangki Fiber said...

You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people could take a signal. Will likely be back to get more. Thanks
Tangki Panel
Tangki Fiberglass
Jual Septic Tank
Tangki Kimia
Jual Talang Fiber
Jual Rotameter
jual mesin ro
Jual Botol Plastik Agro

Jual Botol Plastik Murah

Sophie Grace said...

This is a great blog, I enjoy reading your blog very much! Please dont stop! deskgram

Unknown said...

www1016
ralph lauren polo
christian louboutin
ralph lauren polo
coach outlet
adidas clothing
reebok outlet
canada goose clothing
ralph lauren uk
coach outlet
jordan shoes

yanmaneee said...

kd 11
gucci belt
nike air force 1
balenciaga shoes
kobe byrant shoes
air jordan shoes
yeezy boost 350
lacoste polo
yeezy sneakers
off white

seoexpert said...

Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject?play tetris unblocked

Bandar Togel Online said...

menangkan games di bandar togel online kingdomgrup bosku ada bonus nya http://128.199.187.54/

daidowadford said...

PS Plus free games - Snk21 News
PS Plus free games · Plants vs. Zombies: Battle for Neighborville · Battlefield V 카지노사이트 · Street 바카라사이트 Fighter II: The New Challengers · Super Hang