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

  • SEARCH
  • Home
  • 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 631675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:59:31+00:00 2026-05-13T19:59:31+00:00

I can’t figure out a discrepancy between the time it takes for the Contains

  • 0

I can’t figure out a discrepancy between the time it takes for the Contains method to find an element in an ArrayList and the time it takes for a small function that I wrote to do the same thing. The documentation states that Contains performs a linear search, so it’s supposed to be in O(n) and not any other faster method. However, while the exact values may not be relevant, the Contains method returns in 00:00:00.1087087 seconds while my function takes 00:00:00.1876165. It might not be much, but this difference becomes more evident when dealing with even larger arrays. What am I missing and how should I write my function to match Contains‘s performances?

I’m using C# on .NET 3.5.

public partial class Window1 : Window
{
    public bool DoesContain(ArrayList list, object element)
    {
        for (int i = 0; i < list.Count; i++)
            if (list[i].Equals(element)) return true;

        return false;
    }

    public Window1()
    {
        InitializeComponent();

        ArrayList list = new ArrayList();
        for (int i = 0; i < 10000000; i++) list.Add("zzz " + i);

        Stopwatch sw = new Stopwatch();
        sw.Start();

        //Console.Out.WriteLine(list.Contains("zzz 9000000") + " " + sw.Elapsed);
        Console.Out.WriteLine(DoesContain(list, "zzz 9000000") + " " + sw.Elapsed);
    }
}

EDIT:

Okay, now, lads, look:

public partial class Window1 : Window
{
    public bool DoesContain(ArrayList list, object element)
    {
        int count = list.Count;
        for (int i = count - 1; i >= 0; i--)
            if (element.Equals(list[i])) return true;

        return false;
    }


    public bool DoesContain1(ArrayList list, object element)
    {
        int count = list.Count;
        for (int i = 0; i < count; i++)
            if (element.Equals(list[i])) return true;

        return false;
    }

    public Window1()
    {
        InitializeComponent();

        ArrayList list = new ArrayList();
        for (int i = 0; i < 10000000; i++) list.Add("zzz " + i);

        Stopwatch sw = new Stopwatch();
        long total = 0;
        int nr = 100;

        for (int i = 0; i < nr; i++)
        {
            sw.Reset();
            sw.Start();
            DoesContain(list,"zzz");
            total += sw.ElapsedMilliseconds;
        }
        Console.Out.WriteLine(total / nr);


        total = 0;
        for (int i = 0; i < nr; i++)
        {
            sw.Reset();
            sw.Start();
            DoesContain1(list, "zzz");
            total += sw.ElapsedMilliseconds;
        }
        Console.Out.WriteLine(total / nr);


        total = 0;
        for (int i = 0; i < nr; i++)
        {
            sw.Reset();
            sw.Start();
            list.Contains("zzz");
            total += sw.ElapsedMilliseconds;
        }
        Console.Out.WriteLine(total / nr);
    }
  }

I made an average of 100 running times for two versions of my function(forward and backward loop) and for the default Contains function. The times I’ve got are 136 and
133 milliseconds for my functions and a distant winner of 87 for the Contains version. Well now, if before you could argue that the data was scarce and I based my conclusions on a first, isolated run, what do you say about this test? Not does only on average Contains perform better, but it achieves consistently better results in each run. So, is there some kind of disadvantage in here for 3rd party functions, or what?

  • 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-13T19:59:31+00:00Added an answer on May 13, 2026 at 7:59 pm

    Using the code below I was able to get the following timings relatively consitently (within a few ms):
    1: 190ms DoesContainRev
    2: 198ms DoesContainRev1
    3: 188ms DoesContainFwd
    4: 203ms DoesContainFwd1
    5: 199ms Contains

    Several things to notice here.

    1. This is run with release compiled code from the commandline. Many people make the mistake of benchmarking code inside the Visual Studio debugging environment, not to say anyone here did but something to be careful of.

    2. The list[i].Equals(element) appears to be just a bit slower than element.Equals(list[i]).

      using System;
      using System.Diagnostics;
      using System.Collections;
      
      
      namespace ArrayListBenchmark
      {
      class Program
      {
          static void Main(string[] args)
          {
              Stopwatch sw = new Stopwatch();
              const int arrayCount = 10000000;
              ArrayList list = new ArrayList(arrayCount);
              for (int i = 0; i < arrayCount; i++) list.Add("zzz " + i);
          sw.Start();
          DoesContainRev(list, "zzz");
          sw.Stop();
          Console.WriteLine(String.Format("1: {0}", sw.ElapsedMilliseconds));
          sw.Reset();
      
          sw.Start();
          DoesContainRev1(list, "zzz");
          sw.Stop();
          Console.WriteLine(String.Format("2: {0}", sw.ElapsedMilliseconds));
          sw.Reset();
      
          sw.Start();
          DoesContainFwd(list, "zzz");
          sw.Stop();
          Console.WriteLine(String.Format("3: {0}", sw.ElapsedMilliseconds));
          sw.Reset();
      
          sw.Start();
          DoesContainFwd1(list, "zzz");
          sw.Stop();
          Console.WriteLine(String.Format("4: {0}", sw.ElapsedMilliseconds));
          sw.Reset();
      
          sw.Start();
          list.Contains("zzz");
          sw.Stop();
          Console.WriteLine(String.Format("5: {0}", sw.ElapsedMilliseconds));
          sw.Reset();
      
          Console.ReadKey();
      }
      public static bool DoesContainRev(ArrayList list, object element)
      {
          int count = list.Count;
          for (int i = count - 1; i >= 0; i--)
              if (element.Equals(list[i])) return true;
      
          return false;
      }
      public static bool DoesContainFwd(ArrayList list, object element)
      {
          int count = list.Count;
          for (int i = 0; i < count; i++)
              if (element.Equals(list[i])) return true;
      
          return false;
      }
      public static bool DoesContainRev1(ArrayList list, object element)
      {
          int count = list.Count;
          for (int i = count - 1; i >= 0; i--)
              if (list[i].Equals(element)) return true;
      
          return false;
      }
      public static bool DoesContainFwd1(ArrayList list, object element)
      {
          int count = list.Count;
          for (int i = 0; i < count; i++)
              if (list[i].Equals(element)) return true;
      
          return false;
      }
               }
              }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help me trying to find out why this doesn't work. The brushes
Can anyone point me out or explain what is the difference between android Home
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you tell me what is the difference between abstraction and information hiding in
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I put a Raphael.js canvas over an IMG element? What should I do
Can anyone verify my dealloc method is correct? Since my titleLabel and checkImageView are
Can somebody point me to a resource that explains how to go about having
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump

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.