-
-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Events in this case to act slowly #218
Comments
Hey all, I'm experiencing the same behavior on the latest version 1.2.2. <?php
use parallel\Channel;
use parallel\Events;
use parallel\Runtime;
$total_threads = $argv[1] ?? 0;
if(!$total_threads) die("Please provide number of threads\n");
$threads = [];
$channel = new Channel(100);
for($i = 0; $i < $total_threads; $i++) {
$threads[] = (new Runtime())->run(static function(Channel $channel) {
$events = new Events();
$events->addChannel($channel);
while(true) {
while(($event = $events->poll())) {
$events->addChannel($event->object);
}
}
}, [$channel]);
}
$start = microtime(true);
for($j = 0; $j < 100000000; $j++) {
if(!($j % 10000000)) {
echo "10m iterations in " . (microtime(true) - $start) . " seconds\n";
$start = microtime(true);
}
} Then running from CLI (few examples): With only 1 thread: # php parallel-tester.php 1
10m iterations in 2.1457672119141E-6 seconds
10m iterations in 0.22435593605042 seconds
10m iterations in 0.23026609420776 seconds
10m iterations in 0.224534034729 seconds
10m iterations in 0.22518491744995 seconds
10m iterations in 0.22755908966064 seconds
10m iterations in 0.22358298301697 seconds
10m iterations in 0.24414205551147 seconds
10m iterations in 0.23095417022705 seconds
10m iterations in 0.22916793823242 seconds With 10 threads (there isn't any change): # php parallel-tester.php 10
10m iterations in 9.5367431640625E-7 seconds
10m iterations in 0.22031497955322 seconds
10m iterations in 0.22988986968994 seconds
10m iterations in 0.22356510162354 seconds
10m iterations in 0.22263288497925 seconds
10m iterations in 0.23073315620422 seconds
10m iterations in 0.22678804397583 seconds
10m iterations in 0.22507619857788 seconds
10m iterations in 0.22656798362732 seconds
10m iterations in 0.22526717185974 seconds With 50 threads (very very slow): # php parallel-tester.php 50
10m iterations in 5.0067901611328E-6 seconds
10m iterations in 1.1272690296173 seconds
10m iterations in 1.1491401195526 seconds
10m iterations in 1.0572350025177 seconds
10m iterations in 1.1068761348724 seconds
10m iterations in 1.009134054184 seconds
10m iterations in 1.1045799255371 seconds
10m iterations in 1.3764989376068 seconds
10m iterations in 1.3563649654388 seconds
10m iterations in 1.3363428115845 seconds Is there something I'm doing wrong with the |
I tried the same scenario above (provided by @idanbenezra ) Maybe @realFlowControl / @krakjoe can advise? |
Hey @idanbenezra, I checked your example and indeed, the call to So in your case, with 50 threads you have 50 threads busy-waiting, which is most likely more than you have CPU cores, so your main thread get's scheduled less often and such takes longer to complete the 10 million iterations. I don't know about the use case you have, so far I was mostly using |
Hey @ziaratban, you might see the same problem, but not as extreme, as this is just 3 threads and 2 channels. But as I mentioned in the earlier comment, |
Hi @idanbenezra, I tested your script and got the same results. The fact is the duration you get do not surprise me much: With 50 threads, your operating system (Linux I guess ?) scheduler is having a hard time switching from one thread to the other and this slows down you main thread too. As a matter of fact I notice performance starts to degrade exactly at 11 threads and remains constant (does not degrade much) up to 50 threads. My opinion is : this is (Linux-)scheduler-related. To be more precise: I used command
Maybe this post gives an explanation : https://unix.stackexchange.com/questions/80424/why-using-more-threads-makes-it-slower-than-using-less-threads |
Hi
The text was updated successfully, but these errors were encountered: