Two Weird Phenomena in C# Word Interop Table AddRows and DeleteRows Performance Benchmark Test

Dan Xiao 406 Reputation points
2021-09-02T03:55:13.993+00:00

Dear Sir/Madam,

To address the C# Word Interop Word.Table.AddRows() method performance issue, we are trying to apply the Word.Table.DeleteRows() method instead, upon a Word table template. This is because per our perception, Word.Table.DeleteRows() method should have much faster speed than the Table.AddRows() method, because the AddRows() method needs to take care of each piece of the RichText format of a row in the template table, but the DeleteRows() method would just simply delete, without taking care of any RichText format.

Thereafter, we performed a set of benchmark test between the AddRows() and the DeleteRows() methods, but ended up finding two weird phenomena for explanation.

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?

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?

The C# source codes to do the benchmark test is attached below.

128561-form1.txt

The benchmark test result is attached below for your reference.
128399-benchmarktestresult.txt

Please kindly help to explain the two weird phenomena. Thanks.

Rgds,
Daniel

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,115 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,069 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,611 Reputation points Microsoft Vendor
    2021-09-03T07:12:24.343+00:00

    @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)

    129014-111.gif


    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.