title: Performance difference between Runspace and Job
body: <p>I built a file processor script to convert some files to json. It works but not fast enough, so I am multithreading it. I prefer to use runspace pools since you can specify a max thread limit and it will run that many threads at a time and add new work as it completes other threads, spiffy. But I've found that if I have, say, 6 threads of work to complete, using runspaces takes ~50 minutes and keeps my computer at 40% CPU, while just using Start-Job for each piece of work pegs my computer at 100% CPU, and the work completes in 15 minutes. Am I misconfiguring the runspacepool in some way? Here are simplified examples of each</p>
  
  <pre><code>### Using Start-Job ###
  
  $files = C:\temp | Get-Childitem -filter '*.xel' # returns 6 items
  
  foreach ($file in $files) {
  
      #simplified
      Start-Job -ScriptBlock { C:\temp\FileProcessor.ps1 -filepath $using:file.fullname }
  
  }
  
  
  
  
  ### Using Runspace Pool ###
  
  $files = C:\temp | Get-Childitem -filter '*.xel' # returns 6 items
  
  $Code = {
      param ($filepath)
  
      #simplified
      C:\temp\FileProcessor.ps1 -filepath $filepath
  
  }
  
  $rsPool = [runspacefactory]::CreateRunspacePool(1,100)
  $rsPool.Open()
  
  $threads = @()
  
  foreach ($file in $files) {
  
          $PSinstance = [powershell]::Create().AddScript($Code).AddArgument($file.FullName)
          $PSinstance.RunspacePool = $rsPool
          $threads += $PSinstance.BeginInvoke()    
  
  }
  
  while ($threads.IsCompleted -contains $False) {}
  
  $rsPool.Dispose()
  </code></pre>
  
  <p>I may also be misunderstanding runspaces compared to jobs, any help is welcome. Thank you!</p>