Poor `For Each` performance in Debug mode: Can the speed of iteration be increased?

Is it possible to increase the speed of For Each iteration in Debug mode?

It takes over 4 minutes to iterate over 1,000 integers (248 seconds). The equivalent PowerShell takes 13 milliseconds on the same machine.

EDIT: This apparently applies only to debugging. Run File executes the code in 4 milliseconds. So, is it possible to get the performance closer to run mode when debugging? We have a workflow that iterates over several thousand items that’s not feasible to debug at debug mode speed.

  • Studio version: 2021.4.4, enterprise license

  • Steps to reproduce:

  • Expected behavior:

    • The above workflow takes less than a second to run.

    • The above workflow takes about 4 minutes to run.

  • Equivalent PowerShell (13 milliseconds):

    $total = 0
    
    $watch = [Diagnostics.Stopwatch]::new()
    $watch.Start()
    
    for ($i = 0; $i -lt 1000; $i++)
    {
        $total++
    }
    
    $watch.Stop()
    
    Write-Host "Got to $total in $($watch.Elapsed.TotalMilliseconds) millisecond(s)"
    

Hi,

It’s due to overhead of processing Activity.

There are some ways to improve performance as the following, for example.

1. if you run “Debug File” or “Debug” , try “Run File” or “Run”
It’ll be a little better.

2.Use LINQ

total = total +Enumerable.Range(0,1000).Sum(Function(i) 1)

3. Use Invoke Code activity.

Regards,

Thanks @Yoichi,

#1 looks like it will work for us. We can’t use #2 or #3 because the actual loop body has a bunch of Invoke Workflow File activities and we want to keep the workflows separate for reuse and testing.

I was curious if there was a general-purpose cause and solution. For example, AutoHotkey waits 10ms between each line that it executes, and that can be disabled using SetBatchLines -1. But it sounds like that’s not what’s happening here…It’s the overhead of debugging that slows it down.

If that’s the case, I’m hearing you say the solution is, just don’t debug if there are a lot of items in the set…or debug with test data that has fewer items…right? That’s fine if so.

One complication is that our workflow has parameters. Run File doesn’t let us supply arguments for those as far as I can tell. So after your post, I tried using UiRobot.exe:

UiRobot execute -f 'Composers.xaml' --input "{'in_First': 'Jacob', 'in_Last': 'Gershowitz' }"

That lets us test with the full data set when we need to, though not debug. Just mentioning it for anyone else who might be in this situation.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.