← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #693505]: java multithreading is not parallel

 

Question #693505 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/693505

    Status: Open => Answered

RaiMan proposed the following answer:
This is my test program (latest Java 8 on macOS 10.15 (iMac: 3 GHz
6-Core Intel Core i5))

package com.sikulix.testAPI;

import org.sikuli.script.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Run {
  public static void main(String[] args) {
    ImagePath.add("com.sikulix.testAPI.Run/images");
    Screen screen = new Screen();

    int regw = 300;
    int regh = 300;
    Region reg = new Region(0, 0, regw, regh);
    //to run the image search global init (adds to the first search)
    Image img = Image.create("img");
    reg.has(img);

    System.out.println(String.format("***** region: %dx%d", regw, regh));
    doit(regw, regh, 1, img);
    doit(regw, regh, 10, img);
    doit(regw, regh, 100, img);

    regw = screen.w;
    regh = screen.h;
    System.out.println(String.format("***** region: %dx%d", regw, regh));
    doit(regw, regh, 1, img);
    doit(regw, regh, 10, img);
    doit(regw, regh, 100, img);
  }

  private static void doit(int regw, int regh, int nmax, Image img) {
    long start = new Date().getTime();
    for (int n = 0; n < nmax; n++) {
      Region reg = new Region(0, 0, regw, regh);
      reg.has("img");
    }
    long duration = new Date().getTime() - start;
    System.out.println("nmax: " + nmax);
    System.out.println("duration: " + duration);

    List<Thread> threads = new ArrayList<>();
    for (int n = 0; n < nmax; n++) {
      threads.add(new Thread(() -> {
        Region regt = new Region(0, 0, regw, regh);
        regt.has(img);
      }));
    }
    start = new Date().getTime();
    for (Thread thx : threads) {
      thx.start();
    }
    for (Thread thx : threads) {
      try {
        thx.join();
      } catch (InterruptedException e) {
      }
    }
    duration = new Date().getTime() - start;
    System.out.println("duration threads: " + duration);
  }
}

This is the output:
***** region: 300x300
nmax: 1
duration: 16
duration threads: 17
nmax: 10
duration: 160
duration threads: 104
nmax: 100
duration: 1539
duration threads: 908
***** region: 2048x1152
nmax: 1
duration: 324
duration threads: 333
nmax: 10
duration: 3103
duration threads: 1675
nmax: 100
duration: 29967
duration threads: 14638

Which meets my experiences that I made with the implementation of
Region.findAny() (internally uses a similar construct with threads):
Getting down to 50% elapsed time is the best you can get.

I guess this is due to some internal resource-locking, probably in the
AWT-Robot when capturing the screen.

I will keep an eye on this on the way to the final 2.0.5.

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.