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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:18:20+00:00 2026-05-21T19:18:20+00:00

I’m trying to test what built-in collections perform best under certain applications, such as

  • 0

I’m trying to test what built-in collections perform best under certain applications, such as intersection. To do so, I built the following test:

private static void Main(string[] args)
{
    LoadTest<HashSet<object>>();
    ClearEverythingHere(); // <<-- what can go here?
    LoadTest<LinkedList<object>>();
    Console.ReadKey(true);
}

private static void LoadTest<T>() where T : ICollection<object>, new()
{
    const int n = 1 << 16;
    const int c = 1 << 3;

    var objs = new object[n << 1];
    for (int i = 0; i < n << 1; i++)
        objs[i] = new object();

    var array = new T[c];
    var r = new Random(123);
    for (int s = 0; s < c; s++)
    {
        array[s] = new T();
        for (int i = 0; i < n; i++)
            array[s].Add(objs[r.Next(n << 1)]);
    }

    var sw = Stopwatch.StartNew();
    IEnumerable<object> final = array[0];
    for (int s = 1; s < c; s++)
        final = final.Intersect(array[s]);
    sw.Stop();
    Console.WriteLine("Ticks elapsed: {0}", sw.ElapsedTicks);
}

If I uncomment both test methods from Main, the second test always completes much faster than the first, no matter which order I test the structures. Generally, the first intersection runs in a few hundred ticks, and the second finishes in less than ten. I would have thought having the tests in completely separate scopes would have prevented at least some of the (what I’m presuming is) caching that leads to such different results.

Is there an easy way to reset the application so that I don’t have to worry about caching or optimizing for testing? I would like to be able to run one test, print the results, clear it out, and run another test? Yes, I could comment and uncomment, or possibly spawn two separate applications, but that’s a lot of work for simple console tests.


Edit: I’ve modified the tests as per the suggestions in the answers.

private static void Main(string[] args)
{
    const int n = 1 << 17;
    const int c = 1 << 4;

    var objs = new Item[n << 1];
    for (int i = 0; i < (n << 1); i++)
        objs[i] = new Item(i);

    var items = new Item[c][];
    var hash = new HashSet<Item>[c];
    var list = new LinkedList<Item>[c];

    var r = new Random();
    for (int s = 0; s < c; s++)
    {
        items[s] = new Item[n];
        for (int i = 0; i < n; i++)
            items[s][i] = objs[r.Next(n << 1)];
        hash[s] = new HashSet<Item>(items[s]);
        list[s] = new LinkedList<Item>(items[s]);
    }

    Stopwatch stopwatch = Stopwatch.StartNew();
    HashSet<Item> fHash = hash[0];
    for (int s = 1; s < hash.Length; s++)
        fHash.IntersectWith(hash[s]);
    stopwatch.Stop();

    Console.WriteLine("Intersecting values: {0}", fHash.Count);
    Console.WriteLine("Ticks elapsed: {0}", stopwatch.ElapsedTicks);

    stopwatch = Stopwatch.StartNew();
    IEnumerable<Item> iEnum = list[0];
    for (int s = 1; s < list.Length; s++)
        iEnum = iEnum.Intersect(list[s]);
    Item[] array = iEnum.ToArray();
    stopwatch.Stop();

    Console.WriteLine("Intersecting values: {0}", array.Length);
    Console.WriteLine("Ticks elapsed: {0}", stopwatch.ElapsedTicks);
    Console.ReadKey(true);
}

[DebuggerDisplay("Value = {_value}")]
private class Item
{
    private readonly int _value;

    public Item(int value)
    {
        _value = value;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
            return false;
        if (ReferenceEquals(this, obj))
            return true;
        if (obj.GetType() != typeof(Item))
            return false;
        return Equals(obj);
    }

    public override int GetHashCode()
    {
        return _value;
    }

    public override string ToString()
    {
        return _value.ToString();
    }
}

This solved most of my problems. (And if you’re wondering, HashSet.IntersectWith with appears much faster than IEnumerable.Intersect.)

  • 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-21T19:18:20+00:00Added an answer on May 21, 2026 at 7:18 pm

    There are few errors in your code.

    1. Intersects is LINQ functions, so it means it is lazily evaluated. That means it gets executed only when the data is accesed. This can be done by either looping over the data or calling ToList or ToArray on this enumerable. By adding this, you get different result
    2. Testing must be always done on same data. Try creating your data outside your test method and pass it as parameter.
    3. First pass of code is usualy considered wrong, because JITing and such.
    4. Try creating your own object and override Equals and GetHashCode. Like this it might not be correct to test it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.