From: Leandro Lucarella Date: Thu, 17 Jun 2010 00:07:31 +0000 (-0300) Subject: Add multicore contention micro benchmark X-Git-Url: https://git.llucax.com/software/dgc/dgcbench.git/commitdiff_plain/f14b9214381d316998b7e6342d24543ff444e4ab?hp=01f5925dacbf2b03a5a511ea854fb775da5229eb Add multicore contention micro benchmark --- diff --git a/micro/multicore.d b/micro/multicore.d new file mode 100644 index 0000000..184fbb9 --- /dev/null +++ b/micro/multicore.d @@ -0,0 +1,71 @@ +// Written by David Schima: +// http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=103563 +// +// Modified by Leandro Lucarella: +// * Removed initial "pause" +// * Removed performance counter +// * Adapted to D1/Tango +// +// Compiled with: -O -inline -release +// +// Timing with affinity for all 4 CPUs: 28390 milliseconds +// Timing with affinity for only 1 CPU: 533 milliseconds +// +// More about contention killing multi-core: +// +// import std.stdio, std.perf, core.thread; +// +// void main() { +// writeln("Set affinity, then press enter."); +// readln(); +// +// auto pc = new PerformanceCounter; +// pc.start; +// +// enum nThreads = 4; +// auto threads = new Thread[nThreads]; +// foreach(ref thread; threads) { +// thread = new Thread(&doStuff); +// thread.start(); +// } +// +// foreach(thread; threads) { +// thread.join(); +// } +// +// pc.stop; +// writeln(pc.milliseconds); +// } +// +// void doStuff() { +// foreach(i; 0..1_000_000) { +// synchronized {} +// } +// } +// +// Timing with affinity for all CPUs: 20772 ms. +// Timing with affinity for 1 CPU: 156 ms. +// +// Post on using spin locks in the GC to avoid contention: +// http://www.digitalmars.com/d/archives/digitalmars/D/More_on_GC_Spinlocks_80485.html + +import tango.core.Thread; + +void main() { + enum { nThreads = 4 }; + auto threads = new Thread[nThreads]; + foreach(ref thread; threads) { + thread = new Thread(&doAppending); + thread.start(); + } + + foreach(thread; threads) + thread.join(); +} + +void doAppending() { + uint[] arr; + for (size_t i = 0; i < 1_000_000; i++) + arr ~= i; +} +