Title: Poor Performance in Foreach -Parallel · Issue #10450 · PowerShell/PowerShell · GitHub
Open Graph Title: Poor Performance in Foreach -Parallel · Issue #10450 · PowerShell/PowerShell
X Title: Poor Performance in Foreach -Parallel · Issue #10450 · PowerShell/PowerShell
Description: It was reported here PowerShell/PowerShell-RFC#194 by @Ayanmullick that The below script runs faster without -parallel than with. (Get-WinEvent -ListLog * -EA SilentlyContinue).Where({$PSItem.recordcount -ne '0'}).Logname| ForEach-Object...
Open Graph Description: It was reported here PowerShell/PowerShell-RFC#194 by @Ayanmullick that The below script runs faster without -parallel than with. (Get-WinEvent -ListLog * -EA SilentlyContinue).Where({$PSItem.recor...
X Description: It was reported here PowerShell/PowerShell-RFC#194 by @Ayanmullick that The below script runs faster without -parallel than with. (Get-WinEvent -ListLog * -EA SilentlyContinue).Where({$PSItem.recor...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/10450
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Poor Performance in Foreach -Parallel","articleBody":"It was reported here https://github.com/PowerShell/PowerShell-RFC/pull/194 by @Ayanmullick \r\nthat\r\n\r\n\u003e The below script runs faster without -parallel than with.\r\n\u003e \r\n```\r\n(Get-WinEvent -ListLog * -EA SilentlyContinue).Where({$PSItem.recordcount -ne '0'}).Logname|\r\n ForEach-Object -Parallel {Get-WinEvent -FilterHashtable @{logname = \"$PSItem\";Starttime ='8/24/2019 11:00:00';Endtime ='8/24/2019 11:09:00';Level=1,2,3} -EA SilentlyContinue }\r\n```\r\n\u003e It took 18 secs to finish with -parallel and only 5 without.\r\n\r\nThomas Lee put up this case where it was slower. https://twitter.com/doctordns/status/1164461929093566465\r\n\r\n**It is understood that parallelizing trivial code in will more overhead than the time saved**\r\n\r\nUsing Start-Parallel from the PowerShell Gallery I ran this to ping every machine on my local subnet. 200 addresses are not used, so in sequence it would take 800-1000 seconds. \r\n\r\n`1..254 | Start-Parallel -Scriptblock {param ($P) ping \"192.168.0.$p\" -n 1 | where {$_ -match \"ttl=\"}} -MaxThreads 300`\r\n\r\nit took **9 seconds**\r\n\r\nThe new equivalent is\r\n`1..254 | foreach -Parallel {ping \"192.168.0.$_\" -n 1 | where {$_ -match \"ttl=\"}} -ThrottleLimit 300`\r\n\r\nWhich takes **31 seconds**; I would expect broadly similar performance with the cmdlet slightly faster. \r\n\r\nUsing threadjob which ships with PS 6 and 7 I created this \r\n```\r\n$j = 1..254 | foreach { \r\n $sb=[scriptblock]::Create(\"ping -n 1 192.168.0.$_ | where {`$_ -match 'TTL=' } \") \r\n Start-ThreadJob -ScriptBlock $sb -ThrottleLimit 300 }\r\n while ($j.state -eq \"running\" -or $j.hasmoredata -eq $true) {\r\nforeach ($finished in $j.where({$_.state -eq \"completed\"})){ receive-job -Id $finished.id; } } \r\n\r\n```\r\nWhich runs in 11 seconds.\r\nThe cmdlet should be doing something very similar to this but not taking 3 times as long\r\n\r\n\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/jhoneill","@type":"Person","name":"jhoneill"},"datePublished":"2019-08-27T09:12:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/10450/PowerShell/issues/10450"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c46f4476-49eb-4ba8-fda9-cc72c2a71c53 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8880:1B57E1:A3AC02E:D96D64D:696E215E |
| html-safe-nonce | 2344104a157790ede4abb5f6124c9f963fe5394520ff91283ee6e593c633232a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4ODgwOjFCNTdFMTpBM0FDMDJFOkQ5NkQ2NEQ6Njk2RTIxNUUiLCJ2aXNpdG9yX2lkIjoiMTMyODIwNTkzNzgzODUzMDkxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c97ace63eabd732082b77dbdc1eafe176ac4997b9fe04af08ca1d78577e00ad0 |
| hovercard-subject-tag | issue:485685684 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/10450/issue_layout |
| twitter:image | https://opengraph.githubassets.com/16265bcdb8efccf7b7e43b12e27209d305f5a62cd8823e7724ca11d538b8f533/PowerShell/PowerShell/issues/10450 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/16265bcdb8efccf7b7e43b12e27209d305f5a62cd8823e7724ca11d538b8f533/PowerShell/PowerShell/issues/10450 |
| og:image:alt | It was reported here PowerShell/PowerShell-RFC#194 by @Ayanmullick that The below script runs faster without -parallel than with. (Get-WinEvent -ListLog * -EA SilentlyContinue).Where({$PSItem.recor... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jhoneill |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2fbe8cba5e260284c10af515699ff9bb2d6ace05ab6c2e2e585b71d93b2812c3 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7fb3bc5c995a876085291706b75cf9b08900c338 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width