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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:49:06+00:00 2026-05-26T07:49:06+00:00

I want to do a performance test on how fast an ArrayList(System.Collections – C#)

  • 0

I want to do a performance test on how fast an ArrayList(System.Collections – C#) can insert an item at the beginning.

I’ve opened a file for reading lines of data from, set up a Stopwatch and also created an ArrayList to add items to(as follows):

Stopwatch watchTime = new Stopwatch();
Double totalTime = 0; 
using (StreamReader readText = new StreamReader("data.txt"))
{
    String line;
    Int32 counter = 0; 
    while ((line = readText.ReadLine()) != null)
    {
    }
}

I use the counter to keep track of how many items i’m entering into the ArrayList.

Within the while loop i have the following:

watchTime.Start();
theList.Insert(0, line);
watchTime.Stop();
Double time = watchTime.Elapsed.TotalMilliseconds;
totalTime = totalTime + time; 
Console.WriteLine(time);
watchTime.Reset();
++counter; 

Is this a correct way of checking how fast inserting items into the beginning of the ArrayList occurs??

I made another program that does the exact same thing – however using a Dictionary. To my surprise, the time it takes for this ArrayList to insert items is much longer than the amount of time the Dictionary takes. Why is this happening?

  • 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-26T07:49:07+00:00Added an answer on May 26, 2026 at 7:49 am

    Well, I would suggest:

    • Don’t use files to get input. Why introduce IO into the system?
    • Instead of repeatedly stopping and starting the stopwatch, just insert lots of lines into the ArrayList without doing anything else. Time that big loop in one go.

    As for why Dictionary<,> is cheaper – you didn’t show any code, but basically your insertion code will have to copy the entire contents of the ArrayList on every insertion. ArrayList maintains an array to hold the contents of the list. Usually the array is larger than the list – when you add an element at the end, if can just assign the new value into the right bit of the array. If you insert it elsewhere, it has to copy elements of the array to “make room” for the new element.

    You’d find it a lot quicker adding at the end. Dictionary<,> uses a completely different data structure; it has to resize at some points, but it will have very different characteristics in general.

    (I would suggest you use List<T> instead of ArrayList to start with, and if you want a collection you can insert at the start of repeatedly, consider a LinkedList<T> – or possibly a queue or stack, depending on what you want to do with it later.)

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

Sidebar

Related Questions

I want to know if there is any free tool to test the performance
I want to use performance monitors to determine when a file is being accessed
For performance purposes I want to write to memory, where an external application can
I want to test the performance variations that may happen if memory is allocated
I want to test performance (request per sec) of haskell warp http server. I
I want to conduct a stress and performance test on the front-end of my
I want to test the performance improvement that I could get by using Google's
I have some performance test results on the database, and what I want to
I have a search engine and I want to test its performance. I want
I am doing a performance comparison test. I want to record the run time

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.