ImportRow vs Merge Speed ​​Question

For my own edification, I decided to check the comparative speeds of DataTable.ImportRow and DataTable.Merge. I found that DataTable.ImportRow was significantly slower than DataTable.Merge. In rare cases, both functions had equal processing times. In rare cases, ImportRow was faster than Merge.

Below are the test results and code.

  • Why is ImportRow slower than Merge?
  • What makes Merge faster?

alt text

DataTable dt = new DataTable(); dt.Columns.Add("customerId", typeof(int)); dt.Columns.Add("username", typeof(string)); for (int i = 0; i <= 100000; i++) { DataRow myNewRow; myNewRow = dt.NewRow(); myNewRow["customerId"] = 1; myNewRow["username"] = "johndoe"; dt.Rows.Add(myNewRow); } // First Duration DateTime startTime1 = DateTime.Now; DataTable dt2 = new DataTable(); dt2 = dt.Clone(); for (int i = 0; i < dt.Rows.Count; i++) dt2.ImportRow(dt.Rows[i]); DateTime stopTime1 = DateTime.Now; // End First Duration TimeSpan duration1 = stopTime1 - startTime1; // Second Duration DateTime startTime2 = DateTime.Now; DataTable dt3 = new DataTable(); dt3 = dt.Clone(); dt3.Merge(dt); DateTime stopTime2 = DateTime.Now; // End Second Duration TimeSpan duration2 = stopTime2 - startTime2; 

Edit: updated code as recommended -

  DataTable dt = new DataTable(); dt.Columns.Add("customerId", typeof(int)); dt.Columns.Add("username", typeof(string)); DataColumn[] key = new DataColumn[1]; key[0] = dt.Columns[0]; dt.PrimaryKey = key; for (int i = 0; i <= 100000; i++) { DataRow myNewRow; myNewRow = dt.NewRow(); myNewRow["customerId"] = i; myNewRow["username"] = "johndoe"; dt.Rows.Add(myNewRow); } // First Duration //DateTime startTime1 = DateTime.Now; Stopwatch sw1 = new Stopwatch(); sw1.Start(); DataTable dt2 = new DataTable(); dt2 = dt.Clone(); for (int i = 0; i < dt.Rows.Count; i++) dt2.ImportRow(dt.Rows[i]); //DateTime stopTime1 = DateTime.Now; sw1.Stop(); // End First Duration TimeSpan duration1 = sw1.Elapsed; // Second Duration //DateTime startTime2 = DateTime.Now; Stopwatch sw2 = new Stopwatch(); sw2.Start(); DataTable dt3 = new DataTable(); dt3 = dt.Clone(); dt3.Merge(dt); sw2.Stop(); //DateTime stopTime2 = DateTime.Now; // End Second Duration TimeSpan duration2 = sw2.Elapsed; label3.Text = duration1.Milliseconds.ToString(); label4.Text = duration2.Milliseconds.ToString(); 

alt text

+4
source share
2 answers
  • Your measured differences are pretty small, especially since you only have a resolution of 20 ms (DateTime). Use StopWatch.

  • You set Id = 1 for all records, so it looks like you don't have the proper primary key. This makes it extremely unrepresentative.

  • Merging should be faster, as it can be optimized for mass actions. Given this, I find the results even more equal.

+4
source

First of all, before doing any specific results, I would use " StopWatch " to perform timings, not DateTime.Now. StopWatch is a much more accurate measurement tool and will get more consistent results.

Otherwise, it is logical to assume that the merger may have optimizations to add, since it is intended to import several lines at once.

+1
source

All Articles