Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

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,

question from:https://stackoverflow.com/questions/65870551/can-i-increase-my-datatable-and-list-iteration-speed-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.........
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...