Hello,
I'm a complete newbie in D and trying to compare with Java. I
implemented a simple test for measuring the throughput in message
passing between threads. I see that Java can pass about 4mil
messages/sec while D only achieves 1mil/sec. I thought that D should
be faster.
The messages are simply integers (which are converted to Integer in Java).
The two programs are attached. I tried compiling the D version with
both dmd and gdc and various optimization flags.
mache
import std.concurrency, std.stdio;
import std.datetime;
const n=100000000;
void main() {
auto tid=spawn(&receiver);
setMaxMailboxSize(tid, 1000, OnCrowding.block);
tid.send(thisTid);
foreach(i; 0..n) {
tid.send(i);
}
writeln("finished sending");
auto s=receiveOnly!(string)();
writeln("received ", s);
}
void receiver() {
auto mainTid=receiveOnly!(Tid)();
StopWatch sw;
sw.start();
long s;
for(auto i=0;i<n;i++) {
auto msg = receiveOnly!(int)();
s+=msg;
//writeln("received ", msg);
}
sw.stop();
writeln("finished receiving");
writefln("received %d messages in %d msec sum=%d speed=%d msg/sec", n, sw.peek().msecs, s, n*1000L/sw.peek().msecs);
mainTid.send("finished");
}
package inutil.local;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ThroughputMpTest {
static long n=10000000;
static BlockingQueue<Integer> queue=new ArrayBlockingQueue<Integer>(1000);
static class Consumer implements Runnable {
@Override
public void run() {
long s=0;
try {
long t0=System.currentTimeMillis();
for(int i=0;i<n;i++) {
int x=queue.take();
s+=x;
}
long t1=System.currentTimeMillis();
double d=t1-t0;
System.out.println(n+" messages received in "+d+" ms, sum="+s+" speed: "+1000*d/n+" microsec/message, "+1000*n/d+" messages/sec");
} catch (Exception e){
e.printStackTrace();
}
}
}
static class Producer implements Runnable {
@Override
public void run() {
try {
for(int i=0;i<n;i++) {
queue.put(i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread t=new Thread(new Consumer());
t.start();
(new Thread(new Producer())).start();
t.join();
}
}