Home > database >  How to speed up foreach loop for IEnumerator
How to speed up foreach loop for IEnumerator

Time:02-17

Little background

I am trying to use Intersystems IRIS Api In case you want to see it. https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=BNETNAT_refapi#BNETNAT_refapi_iris-iterator

So basically I have something like this (not the actual code)

string global = "myGlobal";
object subs = new object[2];
subs[0]="node1";
subs[1]="node2";
IRISIterator iter = iris.GetIRISIterator(global, subs); //Returns IEnumerator 

foreach(var item in iter)
{
       //for simplicity just printing it, actual code will process the data
            Console.WriteLine(iter.CurrentSubscript.ToString());
}

It runs very slow, it takes almost 4 seconds to read 50 records. So my question is that given I do not have any control on GetIRISIterator, is it even possible to improve the performance of the above code? Can I do some type of parallel processing or asynchronous execution to reduce the time?

CodePudding user response:

It is possible that the iterator does some slow operation each time the .MoveNext() method is called to advance to the next item. This may be caused by overhead accessing some external system, like a database, and it is possible this overhead is incurred for each item.

So the first thing I would attempt is to not use GetIRISIterator, there seem to be a GetIRISList that instead returns a list. It is possible that this only suffers the overhead once for the complete list.

Parallel processing is unlikely to improve anything, since it is the fetching of items that takes time, not the processing of items.

Asynchronous execution will not reduce the time taken, but it might improve user experience by showing the user that the system is working at processing the request and has not just hanged.

As whenever performance is discussed I will recommend doing appropriate measurements. Using a performance profiler might be very helpful since that may inform you where most of this time is spent.

  • Related