WTF! Parallel foreach more slower that normal foreach in multicore CPU ?

Zardoz luis.panadero at gmail.com
Thu Jun 23 05:56:33 PDT 2011


Ok, so why in std.parallelism examples are this :

// Same thing, but use the default work unit size.
    //
    // Timings on an Athlon 64 X2 dual core machine:
    //
    // Parallel foreach:  388 milliseconds
    // Regular foreach:   619 milliseconds
    foreach(i, ref elem; taskPool.parallel(logs)) {
        elem = log(i + 1.0);
    }

Plus, a change my code to make that for same elem, calc log 100000 times in each loop, and now I get  parallel foreach get a bit shorter time that normal foreach....
Change code :

  auto logs = new double[200];
		const num = 2;

		clock_t clk;
		double norm;
		double par;

		writeln("CPUs : ",totalCPUs );
foreach (t; 0..num) {

	    foreach(i, ref elem; logs) {
	    	foreach(p; 0..100_000)
	        elem = log(i + 1.0);
	    }
		}
		norm = clock() -clk;

		clk = clock();
		foreach (t; 0..num) {

	    foreach(i, ref elem; taskPool.parallel(logs, 100)) {
	    	foreach(p; 0..100_000)
	        elem = log(i + 1.0);
	    }

    }

New times : Normal : 12.725 Parallel : 12.499


More information about the Digitalmars-d-learn mailing list