java - Stepping over from Concurrency to Parallel processing -
i have quad core cpu, , i've been trying experiment multithreading performance reasons. i've written code below see how fast go, , noticed it's slower 2nd code block uses main thread
int numcrunchers = runtime.getruntime().availableprocessors(); public void crunch() { int numpairs = 1000; for(int i=0; < numpairs; i++) pairs.add(...); int share = pairs.size()/numcrunchers; for(int i=0; < numcrunchers; i++) { cruncher cruncher = crunchers.get(i); for(int j=0; j < share; j++) cruncher.nodes.add(pairs.poll()); } for(cruncher cruncher : crunchers) threadpool.execute(cruncher); threadpool.shutdown(); try { threadpool.awaittermination(long.max_value, timeunit.nanoseconds); } catch (interruptedexception e) { e.printstacktrace(); } } private class cruncher implements runnable { public blockingqueue<pair<pathnode>> nodes = new linkedblockingqueue<pair<pathnode>>(); private astarpathfinder pathfinder; private linkedlist<pathnode> path = new linkedlist<pathnode>(); public cruncher(gridgraph graph) { pathfinder = new astarpathfinder(graph); } @override public void run() { while(true) { path.clear(); pair<pathnode> pair = nodes.poll(); if(pair != null) { pathfinder.search(path, pair.first(), pair.second()); paths.add(new linkedlist<pathnode>(path)); } else { system.out.println("this cruncher done"); break; } } } }
each thread took around 34,000,000,000 nanoseconds on pc, when decided use no threads except main thread, took 1,090,195,046 nanoseconds, 34x time difference.
linkedlist<pair<pathnode>> pairs = new linkedlist<pair<pathnode>>(); int numpairs = 1000; astarpathfinder pathfinder = new astarpathfinder(graph); for(int i=0; < numpairs; i++) pairs.add(...); long current = system.nanotime(); for(int i=0; < numpairs; i++) { pair<pathnode> pair = pairs.poll(); path.clear(); pathfinder.search(path, pair.first(), pair.second()); } system.out.printf("operation took %d nanoseconds", system.nanotime() - current);
my question why using multiple threads causes program run slow? code not taking advantage of cores on cpu? ran several times, , results similar, (30+)x time difference between multithreading , single-threading
edit: decided measure time of each individual operation on multithreaded
while(true) { path.clear(); pair<pathnode> pair = nodes.poll(); if(pair != null) { long current = system.nanotime(); pathfinder.search(path, pair.first(), pair.second()); paths.add(new linkedlist<pathnode>(path)); system.out.printf("took %d nanoseconds\n", system.nanotime() - current); } else { system.out.println("this cruncher done"); break; } }
and single threaded...
linkedlist<pair<pathnode>> pairs = new linkedlist<pair<pathnode>>(); int numpairs = 1000; astarpathfinder pathfinder = new astarpathfinder(graph); for(int i=0; < numpairs; i++) pairs.add(...); for(int i=0; < numpairs; i++) { long current = system.nanotime(); pair<pathnode> pair = pairs.poll(); path.clear(); pathfinder.search(path, pair.first(), pair.second()); system.out.printf("operation took %d nanoseconds", system.nanotime() - current); }
each cruncher has own astarpathfinder instance, pathfinder.search() couldn't causing blocking between each of threads. multithreaded application still slower.
Comments
Post a Comment