each! vs foreach parallel timings

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 27 11:30:53 PST 2015


I'm doing some re-writing and measuring.  The basic task is to 
take 10K samples (in struct S samples below) and calculate some 
metrics (just per sample for now).  It isn't evident to me how to 
write the parallel foreach in the same format as each!, so I just 
used the loop form that I understood.

Measured times below are for processing three simple metrics 100 
times on 10K samples. This parallel mode could be very useful in 
my work, which involves processing a bunch of hardware 
performance data.


This is on windows, corei5, DMD32 D Compiler v2.069.2, debug 
build.

each! time:59 ms
parallel! time:20 ms

import std.stdio;
import std.algorithm;
import std.conv;
import std.range;
import std.typecons;
import std.parallelism;
import std.array;
import std.traits;
import std.datetime;

struct S { int sn; ulong a; ulong b; ulong c; ulong d; double e; 
ulong f; ulong m1; double m2; double m3;}

void apply_metrics(int i,ref S s){
	with(s){
		m1 = a+b;
		m2 = (c+d)/e;
		m3 = (c+f)/e;
		sn = i;
	}
}

int main()
{

	S[10000] samples;
	// initialize some values
	foreach ( int i, ref s; samples){
		int j=i+1;
		with (s){
			a=j; b=j*2; c=j*3; d=j*4; e=j*10; f=j*5;
		}
	}

	auto sw = StopWatch(AutoStart.yes);
	// apply several functions on each  sample, also number the 
samples
	foreach(j;iota(100))
	samples[].each!((int i, ref a)=>apply_metrics(i,a));
	writeln("each! time:", sw.peek().msecs, " ms");

	auto sw2 = StopWatch(AutoStart.yes);
	// do the same as above, but in parallel
	foreach(j;iota(100))
		foreach( i, ref a; parallel(samples[])){ apply_metrics(i,a);}
	writeln("parallel! time:", sw2.peek().msecs, " ms");
	return 0;
}


More information about the Digitalmars-d-learn mailing list