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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:38:12+00:00 2026-05-25T15:38:12+00:00

The following 3 example codes are identical except for tiny differences, which is commented

  • 0

The following 3 example codes are identical except for tiny differences, which is commented by Look at here. However the speed differs much.

Sample 1: It runs 59 sec in my computer.

namespace AoDtest
{
    class Program
    {
        static bool IsWin(IList<string[]> slotView)
        {
            string[] symbol = new string[3];
            for (int reelID = 0; reelID < 3; reelID++)
            {
                symbol[reelID] = slotView[reelID][1];
            }
            if (symbol.Contains("B")) return true; // Look at here
            // if (symbol.Any(x => x == "B")) return true;
            return false;
        }
        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            int count = 0;
            string reel1 = "J   K   10  S   R   10  K   Q   10  A   Q   K   10  Q   K   R   10  K   R   10  J   R   10  A   Q   R   A   10  J   Q   10  R   K   10  L   S   A   L   10  Q   A   S   Q   A   R   10  K   R   L   10  R   A   S   10  L   Q   A   L   10  S   R   10  Q";
            string reel2 = "L   K   J   B   A   10  Q   L   R   Q   J   L   Q   R   J   Q   10  J   R   L   Q   J   10  B   Q   K   10  L   Q   J   S   Q   10  L   A   Q   L   J   R   Q   10  S   A   10  Q   B   J   A   L   S   K   Q   S   J   10  Q   L   S   Q   L   K   10  R";
            string reel3 = "J   S   A   J   B   Q   K   J   S   2x  R   Q   S   J   R   L   J   S   K   L   J   K   L   S   J   10  B   K   Q   S   J   K   L   A   K   J   A   K   S   10  J   A   R   2x  L   K   J   A   B   K   J   R   K   J   A   K   J   A   L   R   J   K   R";
            string[] myreel1 = reel1.Split('\t');
            string[] myreel2 = reel2.Split('\t');
            string[] myreel3 = reel3.Split('\t');
            for (int n = 0; n < 200; n++)
            {
                for (int i = 0; i < myreel1.Length; i++)
                    for (int j = 0; j < myreel2.Length; j++)
                        for (int k = 0; k < myreel3.Length; k++)
                        {
                            string[][] slotView = new string[3][];
                            for (int m = 0; m < 3; m++)
                            {
                                slotView[m] = new string[2];
                            }
                            slotView[0][1] = myreel1[i];
                            slotView[1][1] = myreel2[j];
                            slotView[2][1] = myreel3[k];

                            if (IsWin(slotView)) count++;
                        }
            }
            watch.Stop();
            Console.WriteLine(count);
            Console.WriteLine((double)reel1.Length * reel2.Length * reel3.Length / count);
            Console.WriteLine(watch.Elapsed);
        }
    }
}

Sample 2: It runes 20 sec in my computer.

namespace AoDtest
{
    class Program
    {
        static bool IsWin(IList<string[]> slotView)
        {
            string[] symbol = new string[3];
            for (int reelID = 0; reelID < 3; reelID++)
            {
                symbol[reelID] = slotView[reelID][1];
            }
            // if (symbol.Contains("B")) return true;
            if (symbol.Any(x => x == "B")) return true; // Look at here  
            return false;
        }
        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            int count = 0;
            string reel1 = "J   K   10  S   R   10  K   Q   10  A   Q   K   10  Q   K   R   10  K   R   10  J   R   10  A   Q   R   A   10  J   Q   10  R   K   10  L   S   A   L   10  Q   A   S   Q   A   R   10  K   R   L   10  R   A   S   10  L   Q   A   L   10  S   R   10  Q";
            string reel2 = "L   K   J   B   A   10  Q   L   R   Q   J   L   Q   R   J   Q   10  J   R   L   Q   J   10  B   Q   K   10  L   Q   J   S   Q   10  L   A   Q   L   J   R   Q   10  S   A   10  Q   B   J   A   L   S   K   Q   S   J   10  Q   L   S   Q   L   K   10  R";
            string reel3 = "J   S   A   J   B   Q   K   J   S   2x  R   Q   S   J   R   L   J   S   K   L   J   K   L   S   J   10  B   K   Q   S   J   K   L   A   K   J   A   K   S   10  J   A   R   2x  L   K   J   A   B   K   J   R   K   J   A   K   J   A   L   R   J   K   R";
            string[] myreel1 = reel1.Split('\t');
            string[] myreel2 = reel2.Split('\t');
            string[] myreel3 = reel3.Split('\t');
            for (int n = 0; n < 200; n++)
            {
                for (int i = 0; i < myreel1.Length; i++)
                    for (int j = 0; j < myreel2.Length; j++)
                        for (int k = 0; k < myreel3.Length; k++)
                        {
                            string[][] slotView = new string[3][];
                            for (int m = 0; m < 3; m++)
                            {
                                slotView[m] = new string[2];
                            }
                            slotView[0][1] = myreel1[i];
                            slotView[1][1] = myreel2[j];
                            slotView[2][1] = myreel3[k];

                            if (IsWin(slotView)) count++;
                        }
            }
            watch.Stop();
            Console.WriteLine(count);
            Console.WriteLine((double)reel1.Length * reel2.Length * reel3.Length / count);
            Console.WriteLine(watch.Elapsed);
        }
    }
}

Sample 3: Now I use Contains() again, but move some code from IsWin into Main, It runs 14 sec now, why it is faster here?

Big Discovery: IList<string> symbol if I switch to string[] symbol then it runs 57 secs.

namespace AoDtest
{
    class Program
    {
        static bool IsWin(IList<string> symbol)
        {
            if (symbol.Contains("B")) return true;
            // if (symbol.Any(x => x == "B")) return true;
            return false;
        }
        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            int count = 0;
            string reel1 = "J   K   10  S   R   10  K   Q   10  A   Q   K   10  Q   K   R   10  K   R   10  J   R   10  A   Q   R   A   10  J   Q   10  R   K   10  L   S   A   L   10  Q   A   S   Q   A   R   10  K   R   L   10  R   A   S   10  L   Q   A   L   10  S   R   10  Q";
            string reel2 = "L   K   J   B   A   10  Q   L   R   Q   J   L   Q   R   J   Q   10  J   R   L   Q   J   10  B   Q   K   10  L   Q   J   S   Q   10  L   A   Q   L   J   R   Q   10  S   A   10  Q   B   J   A   L   S   K   Q   S   J   10  Q   L   S   Q   L   K   10  R";
            string reel3 = "J   S   A   J   B   Q   K   J   S   2x  R   Q   S   J   R   L   J   S   K   L   J   K   L   S   J   10  B   K   Q   S   J   K   L   A   K   J   A   K   S   10  J   A   R   2x  L   K   J   A   B   K   J   R   K   J   A   K   J   A   L   R   J   K   R";
            string[] myreel1 = reel1.Split('\t');
            string[] myreel2 = reel2.Split('\t');
            string[] myreel3 = reel3.Split('\t');
            for (int n = 0; n < 200; n++)
            {
                for (int i = 0; i < myreel1.Length; i++)
                    for (int j = 0; j < myreel2.Length; j++)
                        for (int k = 0; k < myreel3.Length; k++)
                        {
                            string[][] slotView = new string[3][];
                            for (int m = 0; m < 3; m++)
                            {
                                slotView[m] = new string[2];
                            }
                            slotView[0][1] = myreel1[i];
                            slotView[1][1] = myreel2[j];
                            slotView[2][1] = myreel3[k];

                            string[] symbol = new string[3];
                            for (int reelID = 0; reelID < 3; reelID++)
                            {
                                symbol[reelID] = slotView[reelID][1]; // Look at here
                            }

                            if (IsWin(symbol))
                            {
                                count++;
                            }
                        }
            }
            watch.Stop();
            Console.WriteLine(count);
            Console.WriteLine((double)reel1.Length * reel2.Length * reel3.Length / count);
            Console.WriteLine(watch.Elapsed);
        }
    }
}

Sample 4: Similar to Sample 3, but use Any instead, runs 17 sec.
Big Discovery: IList<string> symbol if I switch to string[] symbol then it runs 18 secs.

namespace AoDtest
{
    class Program
    {
        static bool IsWin(IList<string> symbol)
        {
            if (symbol.Contains("B")) return true;
            // if (symbol.Any(x => x == "B")) return true;
            return false;
        }
        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            int count = 0;
            string reel1 = "J   K   10  S   R   10  K   Q   10  A   Q   K   10  Q   K   R   10  K   R   10  J   R   10  A   Q   R   A   10  J   Q   10  R   K   10  L   S   A   L   10  Q   A   S   Q   A   R   10  K   R   L   10  R   A   S   10  L   Q   A   L   10  S   R   10  Q";
            string reel2 = "L   K   J   B   A   10  Q   L   R   Q   J   L   Q   R   J   Q   10  J   R   L   Q   J   10  B   Q   K   10  L   Q   J   S   Q   10  L   A   Q   L   J   R   Q   10  S   A   10  Q   B   J   A   L   S   K   Q   S   J   10  Q   L   S   Q   L   K   10  R";
            string reel3 = "J   S   A   J   B   Q   K   J   S   2x  R   Q   S   J   R   L   J   S   K   L   J   K   L   S   J   10  B   K   Q   S   J   K   L   A   K   J   A   K   S   10  J   A   R   2x  L   K   J   A   B   K   J   R   K   J   A   K   J   A   L   R   J   K   R";
            string[] myreel1 = reel1.Split('\t');
            string[] myreel2 = reel2.Split('\t');
            string[] myreel3 = reel3.Split('\t');
            for (int n = 0; n < 200; n++)
            {
                for (int i = 0; i < myreel1.Length; i++)
                    for (int j = 0; j < myreel2.Length; j++)
                        for (int k = 0; k < myreel3.Length; k++)
                        {
                            string[][] slotView = new string[3][];
                            for (int m = 0; m < 3; m++)
                            {
                                slotView[m] = new string[2];
                            }
                            slotView[0][1] = myreel1[i];
                            slotView[1][1] = myreel2[j];
                            slotView[2][1] = myreel3[k];

                            string[] symbol = new string[3];
                            for (int reelID = 0; reelID < 3; reelID++)
                            {
                                symbol[reelID] = slotView[reelID][1]; // Look at here
                            }

                            if (IsWin(symbol))
                            {
                                count++;
                            }
                        }
            }
            watch.Stop();
            Console.WriteLine(count);
            Console.WriteLine((double)reel1.Length * reel2.Length * reel3.Length / count);
            Console.WriteLine(watch.Elapsed);
        }
    }
}
  • 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-25T15:38:12+00:00Added an answer on May 25, 2026 at 3:38 pm

    This is rather interesting.

    Documentation for IEnumerable<T>.Contains says that if the source parameter implements ICollection<T>, then it calls ICollection<T>.Contains. And since string[] implements ICollection<string>, that’s what gets called.

    The Array implementation of ICollection<T>.Contains ends up calling Array.IndexOf.

    If you change your code to read:

    if (Array.IndexOf(symbol, "B"))
    

    It executes as fast as Any. Same if you change the code to:

    if ((symbol as ICollection<string>).Contains("B"))
    

    In my tests, calling Array.IndexOf is twice as fast as calling symbol.Contains.

    I suspect what’s slowing things down is that IEnumerable<T>.Contains has to decide with each call whether it will call ICollection<T>.Contains, or do something else. That decision doesn’t have to be made when Any is called.

    You can replace all the code in IsWin with this, which is faster still than either of the above, and is a whole lot simpler.

    static bool IsWin(IList<string[]> slotView)
    {
        return slotView.Any(s => s[1] == "B");
    }
    

    Then, of course, you can just get rid of the IsWin method altogether and write in your inner loop:

    if (slotView.Any(s => s[1] == "B")
        ++count;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following two pieces of code which i think should be identical
When I compiled the sample codes of C++, I got following info: c++ excxx_example_database_read.cpp
I have the following example code: class A(object): def __init__(self, id): self.myid = id
The following Microsoft example code contains the following: <Grid> ... <Border Name=Content ... >
In the following code example how do the user/userParam references relate to the Customer
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
The C# 3.0 spec has the following code example in section 10.6.1.3 Output parameters:
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
Take the following code for example; if (Convert.ToString(frm.WindowState) == Minimized) Layout.WindowState = Maximized; else

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.