@Dan Xiao , based on my test, I reproduced your first problem.
Here is a code example I made.
private void button1_Click(object sender, EventArgs e)
{
Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Open("D:\\2.docx");
Word.Table table = doc.Tables[1];
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < numericUpDown1.Value; i++)
{
table.Rows.Add();
}
watch.Stop();
doc.Save();
doc.Close();
textBox1.Text = watch.ElapsedMilliseconds.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Open("D:\\2.docx");
Word.Table table = doc.Tables[1];
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < numericUpDown1.Value; i++)
{
table.Rows[table.Rows.Count].Delete();
}
watch.Stop();
doc.Save();
doc.Close();
textBox2.Text = watch.ElapsedMilliseconds.ToString();
}
public Form1()
{
InitializeComponent();
numericUpDown1.Maximum = 2000;
}
1) The overall performance of the DeleteRows() method is somehow lower than the AddRows() method. From the test result, except for the scenario of 150 rows. This is really out of our common sense. So may we know the internal mechanism of AddRows() and DeleteRows() methods? Why the DeleteRows() method is poorer than the AddRows() method abnormally?
According to my test, I find that the Rows.Delete Method is always lower than the Rows.Add Method. Rows.Delete method need to use the current index to delete the row and Rows.Add could be added to the last row directly.
2) In the test results of the AddRows() method, to our surprise, the overall time overhead is not all the way increasing as expected, somehow adding 200 rows can take less time than adding 150 rows! This is quite weird. I doubt that there may be some internal performance optimization measures in the AddRows() method, which can exactly happen between adding 150 and 200 rows. Am I right? Can you try to explain this to us?
I didn't reproduce the problem even if I add or delete 200 rows.
Tested Result:(Button1 is used to add rows Button2 is used to delete rows)
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.