Solved

Improve nested for each performance

Posted on 2013-11-13
1
218 Views
Last Modified: 2013-11-14
Datatable  dt with three columns
ID
BusDate
Return


listReturns(Type string,  Value int)
Daily 1
Weekly 7
Bi Weekly 14
Monthly 30


For very row in dt I need to calculate the daily weekly, bi wekly and monthly returns by finding the Return i1 day ago, 7 days ago or a month ago.
Then divide the value with that of the given date.

For every ID I need to loop four times for each return type.
The nested for each takes an hour for 71000 Rows in dt. I am asuming my linq query to get the tri value is taking some time



                  foreach (DataRow item in dt.Rows)
            {
                                 
                    foreach (var mt in listReturns)
                              {
                                  decimal tri = (from r in dtHistoricalPrices.AsEnumerable()
                                   where r["ID"].ToString() == item["ID"].ToString()
                                   && Convert.ToDateTime(r["BusDate"]) == Convert.ToDateTime(item["BusDate"]).AddDays(-mt.Value)
                                 
                                   && r["Return"] != DBNull.Value && r["Return"].ToSting() != string.Empty
                                   select Convert.ToDecimal(r["Return"]).FirstOrDefault();
                                                   
                                    decimal dreturn =       (Convert.ToDecimal(item["Return"]) / tri) - 1;         
                                  DataRow dr = dt2.NewRow();
                        dr["ID"] = item["ID"];
                        dr["BusDate"] = item["BusDate"];
                                    dr["ReturnType"] = mt.Type;
                                    dr["ReturnValue"] = dreturn
                              }
                  }
0
Comment
Question by:countrymeister
1 Comment
 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 500 total points
ID: 39647259
Are you sure the Convert.ToDateTime(r["BusDate"])  is really necessary?  That needs to be executed again and again, for every row in dtHistoricalPrices, for every value in listReturns, for every row in dt.

Datasets and datatables are easy to work with, but they have a lot of overhead.  In a lot of cases you can improve the performance drastically if you avoid using datasets.  Try getting the data from the database in a collection of objects, with correctly typed properties for each column.  

You could start with the dtHistoricalPrices.  Create a HistoricalPrice class, Make a collection with an object of that type for every row in dtHistoricalPrices, and then do your LINQ query on that collection.  The performance will probably be a lot better.
If necessary, you can do the same thing with the dt datatable.

To start easy, you can convert the data from the datatable to the custom objects, but I would consider using a DataReader to get the data from the databse and create that collection of objects, and avoid the use of datasets and datatables.

Hope this helps.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hey Audio! 3 30
VB.NET 2008 Publish Error 2 27
VB.NET 2008 - SQL Timeout 9 24
MailAddress in vb 4 15
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question