I make a kind of probabilistic simulator that will work either for a certain amount of time or for a certain number of repetitions. I want to optimize it, and it is currently multithreaded with each ProbabilityWorkerextension Thread, and the main program automatically allocates threads nwhere n, however, many threads are available (example: on my Core i3-7100U, this is 4).
I am analyzing the performance of this, and I understand that the methodology that I use to get the current time relative to the end time causes a lot of overhead.
In the mode in which it can "work for a certain time", I made objects new Dateas part of a loop condition, then I changed it to a faster System.currentTimeMillis()one to try and save time, but I notice that it brings to nothing.
My function is runas follows:
public void run() {
if (mode) {
while (completed < repitions) {
resultSet[randy.nextInt(o)]++;
completed++;
}
} else {
while (System.currentTimeMillis() < endTime) {
resultSet[randy.nextInt(o)]++;
completed++;
}
}
done = true;
}
Where modeis true, if it is performed for the number of repetitions, it randyis random, ois the number of possible results, and endTimeis the endpoint in milliseconds, the system time (which can be changed, the program takes several seconds, and is endTimecalculated according to the current time plus secondsInput * 1000).
Also, on the same Core i3-7100U, these are my performance statistics:
DE-WEY-LAPTOP:/mnt/c/Users/danny/Documents/Programming/Data Structures/Probability$ java Main -n 10000000000
Running 10000000000 repitions of the probability simulator with 2 possible outcomes.
4 threads detected on system; doing 2500000000 repitions per thread.
Done. Gathering results from worker threads...
Done. Printing results...
Outcome 1: 4999997330 out of 10000000000 (49.9999733%)
Outcome 2: 5000002670 out of 10000000000 (50.0000267%)
Time taken: 43.443 seconds (2.301866813986143E8 ops/sec)
DE-WEY-LAPTOP:/mnt/c/Users/danny/Documents/Programming/Data Structures/Probability$ java Main -t 44
Running the probability simulator for 44 seconds using 4 threads.
Done. Gathering results from worker threads...
Done. Printing results...
Outcome 1: 141568074 out of 283130850 (50.000935609807264%)
Outcome 2: 141562776 out of 283130850 (49.999064390192736%)
Time taken: 44 seconds (6434792.045454546 ops/sec)
, System.currentTimeMillis() , , ? , ?