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 6713755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:03+00:00 2026-05-26T08:25:03+00:00

While performance testing, I noticed something interesting. I noticed that the very first insertion

  • 0

While performance testing, I noticed something interesting.

I noticed that the very first insertion into a LinkedList(C# Generics) is extremely slower than any other insertion done at the head of the list. I simply used the C# template LinkedList and used AddFirst() for each insertion into the LinkedList. Why is the very first insertion the slowest?


First Five Insertion Results:

First insertion into list: 0.0152 milliseconds

Second insertion into list(at head): 0.0006 milliseconds

Third insertion into list(at head): 0.0003 milliseconds

Fourth insertion into list(at head): 0.0006 milliseconds

Fifth insertion into list(at head): 0.0006 milliseconds

Performance Testing Code:

        using (StreamReader readText = new StreamReader("MillionNumbers.txt"))
        {
            String line;
            Int32 counter = 0; 
            while ((line = readText.ReadLine()) != null)
            {

                watchTime.Start();
                theList.AddFirst(line);
                watchTime.Stop();
                Double time = watchTime.Elapsed.TotalMilliseconds;
                totalTime = totalTime + time; 
                Console.WriteLine(time);
                watchTime.Reset();
                ++counter; 
            }
            Console.WriteLine(totalTime);
            Console.WriteLine(counter);
            Console.WriteLine(totalTime / counter); 
        }
  • 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-05-26T08:25:03+00:00Added an answer on May 26, 2026 at 8:25 am

    Timing a single operation is very dangerous – the slightest stutter can make a huge difference in results. Additionally, it’s not clear that you’ve done anything with LinkedList<T> before this code, which means you’d be timing the JITting of AddFirst and possibly even whole other types involved.

    Timing just the first insert is rather difficult as once you’ve done it, you can’t easily repeat it. However, you can time “insert and remove” repeatedly, as this code does:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    class Program
    {
        public static void Main(string[] args)
        {
            // Make sure we've JITted the LinkedList code
            new LinkedList<string>().AddFirst("ignored");
    
            LinkedList<string> list = new LinkedList<string>();        
            TimeInsert(list);
            list.AddFirst("x");
            TimeInsert(list);
            list.AddFirst("x");
            TimeInsert(list);
            list.AddFirst("x");        
        }
    
        const int Iterations = 100000000;
    
        static void TimeInsert(LinkedList<string> list)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
    
            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0; i < Iterations; i++)
            {
                list.AddFirst("item");
                list.RemoveFirst();
            }
            sw.Stop();
    
            Console.WriteLine("Initial size: {0}; Ticks: {1}",
                               list.Count, sw.ElapsedTicks);
        }
    }
    

    My results:

    Initial size: 0; Ticks: 5589583
    Initial size: 1; Ticks: 8137963
    Initial size: 2; Ticks: 8399579
    

    This is what I’d expect, as depending on the internal representation there’s very slightly more work to do in terms of hooking up the “previous head” when adding and removing to an already-populated list.

    My guess is you’re seeing JIT time, but really your code doesn’t really time accurately enough to be useful, IMO.

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

Sidebar

Related Questions

I'm doing some performance testing of my app and noticed that it takes exceedingly
When testing performance of various web pages on my web application I noticed that
While doing performance testing on windows, i usually use perfmon. But when i put
While testing out a UDP multicast server that I've written on Windows 7 Ultimate
When I ran the profiler on a JQuery intensive page while tuning for performance,
While doing some JavaScript performance tests I came up with the following piece of
With a customer web site we currently experiences performance problems. While analyzing the problem
Jeff Atwood wrote about this here , and while I understand the theoretical performance
I'm doing some performance testing against an Apache server and getting the dreaded message
I am developing a small application using EF 4.0 and POCO. While testing my

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.