Mario Garcia
Mario Garcia

Reputation: 643

big foreach loop getting slower

First, sorry for my English, it's not my main language. Second, I'll do my best trying to explain myself, but if something is unclear I can provide any details. I'm a bit unexperienced, let's start.

I have a big foreach loop, with 39k elements.

foreach (Busqueda.CodDesCIE elemResult in result.listaCodDesCIE)
            {
                timer.Restart();
                elemResultExterno = new getCodBusquedaResponse.ResultadoBusquedaResponse(elemResult.IdBuzon_Detalle, elemResult.Orden, elemResult.TipoAcierto, elemResult.ObjCIE10.Codigo, elemResult.ObjCIE10.Deslarga, elemResult.Cie10Adic1, elemResult.Cie10Adic2);
                listaResult.Add(elemResultExterno);
                param += " IdBuzon_Detalle:" + elemResultExterno.IdBuzonDetalle + " Orden:" + elemResultExterno.Orden + " TipoAcierto:" + elemResultExterno.TipoAcierto + " Codigo:" + elemResultExterno.Codigo + " Descripcion:" + elemResultExterno.Descripcion + " CodAdic1:" + elemResultExterno.CodAdic1 + " CodAdic2:" + elemResult.Cie10Adic2 + "\r\n";
                timer.Stop();
                Logger.Current.InfoLog("timerPartial: " + timer.ElapsedMilliseconds.ToString(), "itemNumber", orden.ToString());

                orden++;
            }

new getCodBusquedaResponse.ResultadoBusquedaResponse()

The problem is that each iteration, takes less than 0ms, for the first... 6k or more elements, but iteration time starts growing at that point, reaching up to 5ms in the final iterations. Ocasionally, I also see some peeks (dunno if you call it like that, "hight points") where an iteration might take 80ms, or 100ms.

I would like to know, why the time keeps growing, if it is normal, if it is easy to avoid...and some help / explanations on how I could optimize this code.

Log: Time is in ms

[INFO] - itemNumber.2() - timerPartial: 0
[INFO] - itemNumber.7666() - timerPartial: 0
[INFO] - itemNumber.7667() - timerPartial: 1
[INFO] - itemNumber.7725() - timerPartial: 81
[INFO] - itemNumber.23579() - timerPartial: 3
[INFO] - itemNumber.24356() - timerPartial: 101
[INFO] - itemNumber.28144() - timerPartial: 5
[INFO] - itemNumber.29201() - timerPartial: 6
[INFO] - itemNumber.33997() - timerPartial: 5
[INFO] - itemNumber.33998() - timerPartial: 6
[INFO] - itemNumber.38547() - timerPartial: 80
foreachLoop.() - timerTotal: 239389

Many thanks in advance.

EDIT: List<getCodBusquedaResponse.ResultadoBusquedaResponse> listaResult

EDIT2: Many of you seem to point out the paramString. This code is not 100% mine so not sure if that is actually needed, will try with some of your suggestions or even commenting that line and give some feedback, thanks.

EDIT3: WOW. Commented the string line: foreachLoop.() - timerTotal: 20343

Didn't knew about this issue. For now, this will make it. For sure, will try CarbineCoder suggestion and Eiver also. If that also helps, then great, any time I can save will be welcome.

Thanks a lot.

Upvotes: 1

Views: 494

Answers (2)

Eiver
Eiver

Reputation: 2645

CarbineCoder answer is one obvious optimization.

The other one being:

var listaResult= new List<getCodBusquedaResponse.ResultadoBusquedaResponse>(YOUR_EXPECTED_CAPACITY);

Try these. If they do not work. Update your question so that it doesn't include methods, that are unknown to us.

Upvotes: 1

Carbine
Carbine

Reputation: 7903

One bad thing I noticed is you are appending param string variable. Which will create a lot of throw away objects within your loop. Instead use a string builder.

StringBuilder builder = new StringBuilder()
foreach (Busqueda.CodDesCIE elemResult in result.listaCodDesCIE)
{
   ..
    builder.Append(" IdBuzon_Detalle:").Append( elemResultExterno.IdBuzonDetalle);//etc.....
}
param = builder.ToString();

Upvotes: 4

Related Questions