1

I've a piece of java code which constructs an object from xml and takes some nanoseconds to a millisecond depending on object size. Sometimes I've to call that method 1-2 times, sometimes 70-80 times in loop to construct a list of objects.

I tried constructing the objects in parallel, but sometimes it's taking double time than sequential and half the other times. Now my question is are there any guidelines or performance comparison metrics to guide when should multitasking be used and when it's just an overkill?

Sample code that I'm using is:

    List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
    for (final Integer object : list) {
        Callable<Integer> c = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                    return test.m1(object);
            }
        };
        tasks.add(c);
    }
    List<Future<Integer>> results = EXEC.invokeAll(tasks);

    for (Future<Integer> fr : results) {
        fr.get();
    }
Heisenberg
  • 5,514
  • 2
  • 32
  • 43

2 Answers2

1

Short Answer: It is overkill when you don't have performance or IO blocking issues.

Couple of the factors about parallel performance are:

  • How much communication/coordination is needed between the tasks. See Embarrassingly parallel for examples with minimal coordination.
  • The structures for parallel processing take time to be created. eg OSX Thread takes about 90 microseconds to be created so you need to save at least that much (if you create one).
  • Parallel processing will not speed up the sequential fraction. If the task takes an hour and only 75% of it can be processed in parallel then, you will not make it complete in less than 15 minutes. See Amdahl's law.
mikek3332002
  • 3,546
  • 4
  • 37
  • 47
  • My tasks don't need any coordination between them. Amdahl's law helps. But I was hoping to get some approximate performance numbers or graphs tested by someone on their machine. If possible, in Java. Say on linux if a method takes 500 microseconds to execute and I need to execute it 100 times in loop then it makes sense to execute them in parallel using fixedThreadPool. – Heisenberg Apr 14 '15 at 07:44
  • @Mr.White If you have performance issues, profile your application first to work out the bottleneck – mikek3332002 Apr 15 '15 at 04:24
1

Take a look at Doug Lea's post "When to use parallel streams".

The rough estimate (within a factor of ten) is 100 microseconds of sequential execution when parallel computation starts making sense. Though there are much more factors to consider.

apangin
  • 92,924
  • 10
  • 193
  • 247