简体   繁体   中英

Can I increase my DataTable and List<> iteration speed. C#

I hope you're all doing well.

I have a quick question regarding iteration. I've read several post about the speed of iteration and I couldn't figure how to make my iteration faster. Currently I'm doing something like this:

void Iteration()
        {
            //Creating and filling the datatable
            DataTable dt = new DataTable();
            dt.Columns.Add("Datetime", typeof(DateTime));
            for (int i = 0; i < 150; i++)
            {
                DataRow row = dt.NewRow();
                row["Datetime"] = DateTime.Now.AddDays(i);
                dt.Rows.Add(row);
            }

            //Creating and filling the list
            List<DateTime> _listDates = new List<DateTime>();
            DateTime _startDate = DateTime.Now.AddMonths(-1);
            for(int i = 0; i < 250; i++)
                _listDates.Add(_startDate.AddDays(i));

            //Here's the actual iteration
            foreach (DateTime _date in _listDates)
            {
                foreach (DataRow row in dt.Rows)
                {
                    if ((DateTime)row["Datetime"] == _date)
                    {
                        //Do something.........
                    }
                }
            }
        }

I fill a List<DateTime> and a DataTable with respectively 250 and 150 rows/line. I then want to compare the two values against each other and do something when there's a match. However, in my method that means 250 * 150 = 37500 passes. Now I could break out the loop when there's a match but that seems trivial to me since the match can also be on the bottom of the list and datatable. And in my program the average lists and tables have 2500 rows. So that's millions of passes every n minutes. Needles to say that this takes a while. I'm running this calculation on a separate thread so my program stays responsive.

Is there any way to make this smarter and/or faster? Am I on the right track?

Cheers,

What about this? this is more efficient because both data table and datetime list are scanned only once, and HashSet.Contains time complexity is O(1).

void Iteration()
{
    //Creating and filling the datatable
    DataTable dt = new DataTable();
    dt.Columns.Add("Datetime", typeof(DateTime));
    for (int i = 0; i < 150; i++)
    {
        DataRow row = dt.NewRow();
        row["Datetime"] = DateTime.Now.AddDays(i);
        dt.Rows.Add(row);
    }

    //Creating and filling the list
    List<DateTime> _listDates = new List<DateTime>();
    DateTime _startDate = DateTime.Now.AddMonths(-1);
    for (int i = 0; i < 250; i++)
        _listDates.Add(_startDate.AddDays(i));

    var dateSet = new HashSet<DateTime>(_listDates);

    foreach (DataRow row in dt.Rows)
    {
        if (dateSet.Contains( (DateTime)row["Datetime"]))
        {
            //Do something.........
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM