Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8527055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:26:52+00:00 2026-06-11T08:26:52+00:00

I’ve made some speed tests concerning Lists in C#. Here is a result that

  • 0

I’ve made some speed tests concerning Lists in C#. Here is a result that I cannot explain. I hope someone can figure out what is happening.

Miliseconds for 1000 iterations if cloneList.RemoveAt(cloneList.Count – 1) is called before cloneList.Add(next): x milliseconds.

Miliseconds for 1000 iterations if cloneList.RemoveAt(cloneList.Count – 1) is NOT called before cloneList.Add(next): at least 20x milliseconds.

It seems if a have one more statement my code get 20 times faster (see the code below):

        Stopwatch stopWatch = new Stopwatch();
        Random random = new Random(100);

        TimeSpan caseOneTimeSpan = new TimeSpan();
        TimeSpan caseTwoTimeSpan = new TimeSpan();


        int len = 1000;

        List<int> myList = new List<int>();
        myList.Capacity = len + 1;

        // filling the list
        for (int i = 0; i < len; i++)
            myList.Add(random.Next(1000));

        // number of tests (1000)
        for (int i = 0; i < 1000; i++)
        {
            List<int> cloneList = myList.ToList();
            int next = random.Next();

            // case 1 - remove last item before adding the new item
            stopWatch.Start();
            cloneList.RemoveAt(cloneList.Count - 1);
            cloneList.Add(next);
            caseOneTimeSpan += stopWatch.Elapsed;

            // reset stopwatch and clone list

            stopWatch.Reset();
            cloneList = myList.ToList();

            // case 2 - add without removing
            stopWatch.Start();
            cloneList.Add(next);
            caseTwoTimeSpan += stopWatch.Elapsed;


            stopWatch.Reset();

        }

        Console.WriteLine("Case 1: " + caseOneTimeSpan.TotalMilliseconds);
        Console.WriteLine("Case 2: " + caseTwoTimeSpan.TotalMilliseconds);
        Console.WriteLine("Case 2 / Case 1: " + caseTwoTimeSpan.TotalMilliseconds / caseOneTimeSpan.TotalMilliseconds);  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:26:54+00:00Added an answer on June 11, 2026 at 8:26 am

    When you add an item to a list there are two possibilities:

    1. The internal buffer is large enough to add another item. The item is placed in the next free location. Speed: O(1) (This is the most common case.)
    2. The internal buffer is not large enough. Create a new, larger, buffer. Copy all items from the old buffer to the new one. Add the next item to the new buffer. Speed: O(n) (this shouldn’t be occurring often)

    While most Add calls will be O(1), some are O(n).

    Removing the last item is always O(1).

    Since Add is sometimes dependent on the size of the list, when the list is larger it takes longer (if any calls require a new buffer). If you always remove items when adding a new one you are ensuring that the internal buffer always has enough space.

    You can look at the Capacity property of List to see the current size of the internal buffer and compare it to Count, which is the number of items that the list actually has. (Therefore Capacity-Count is the number of free items in the buffer.) While not often useful in real programs, looking at these tools when debugging or developing an application can be useful to helping you see what’s going on underneath.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.