locked
Array Items In C# RRS feed

  • Question

  • User-256025523 posted

    Hi All,

             I have array with 10,000 records with numbers. I have to add all the numbers using C#. Which is the fastest way to perform this operation?

      Thanks,

    Prashant N

    Tuesday, December 29, 2015 11:40 AM

Answers

  • User-1078128378 posted

    Hi,

    use  this Method

     private void ArrayAddtion()
            {
                int[] arr = new int[] { 1, 2, 3 };
                int sum = 0;
                sum = arr.Sum();
                Response.Write("sum= " + sum);
            }

    and call this method in your button click event like this

     private void button1_Click(object sender, EventArgs e)
            {
                ArrayAddtion();
            }

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 29, 2015 11:49 AM
  • User-986267747 posted

    Hi pr_nimbalkar,

    I have array with 10,000 records with numbers. I have to add all the numbers using C#. Which is the fastest way to perform this operation?

    In my experience, if you are use .Net 3.5, you could use the following code:

    int sum = arr.Sum();
    Console.WriteLine(sum);

    If you're not using .NET 3.5 you could do this:

    int sum = 0;
    Array.ForEach(arr, delegate(int i) { sum += i; });
    Console.WriteLine(sum);

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 30, 2015 5:35 AM
  • User-434868552 posted

    @pr_nimbalkar

    to know what is faster, one must time the code.

    based only on my limited testing today, on one million operations, here are my results, fastest to slowest:

    1. c.  33":  Array.ForEach(TenK, delegate (int i) { total += i; });
    2. c.  40":  for (int index = 0; index < 10000; index++) total += TenK[index];
    3. c.  54":  total = TenK.Sum();

    The code snippets, shown below, were run in LINQPad 5 on Windows 8.1 intel core i7 16GB RAM

    N.B.:  there may be faster methods.

    void Main()   // fastest
    {
        Int32[] TenK = new Int32[10000];
        for (int index = 0; index < 10000; index++) TenK[index] = index + 1;
        Console.WriteLine((10000 * 10001) / 2); // compute sum of 1, 2, ..., 9999, 10000 using shortcut
        Int32 loops = 1000000;
        DateTime start = DateTime.Now;
        Int32 total = 0;
        for (int outerIndex = 0; outerIndex < loops; outerIndex++)
        {
            total = 0; // re-initialize because we're using +=
            Array.ForEach(TenK, delegate (int i) { total += i; });
        }
        DateTime end = DateTime.Now;
        Console.WriteLine(end - start);
        Console.WriteLine(total);
    }
    void Main()  // second fastest
    {
        Int32[] TenK = new Int32[10000];
        for (int index = 0; index < 10000; index++) TenK[index] = index + 1;
        Console.WriteLine((10000*10001)/2); // compute sum of 1, 2, ..., 9999, 10000 using shortcut
        Int32    loops = 1000000;
        DateTime start = DateTime.Now;
        Int32 total = 0;
        for (int outerIndex = 0; outerIndex < loops; outerIndex++)
        {
            total = 0;   // re-initialize because we're using +=
            for (int index = 0; index < 10000; index++) total += TenK[index];
        }
        DateTime end   = DateTime.Now;
        Console.WriteLine(end - start);
        Console.WriteLine(total);
    }

    EDIT:

    void Main()   // slowest of these 3 examples occurs when using .Sum() method
    {
        Int32[] TenK = new Int32[10000];
        for (int index = 0; index < 10000; index++) TenK[index] = index + 1;
        Console.WriteLine((10000 * 10001) / 2); // compute sum of 1, 2, ..., 9999, 10000 using shortcut
        Int32 loops = 1000000;
        DateTime start = DateTime.Now;
        Int32 total = 0;
        for (int outerIndex = 0; outerIndex < loops; outerIndex++)
            total = TenK.Sum();
        DateTime end = DateTime.Now;
        Console.WriteLine(end - start);
        Console.WriteLine(total);
    }

    purpose of edit:  to fix copy and paste error for the last example.  mea culpa

    END EDIT.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 30, 2015 10:10 PM

All replies

  • User-1078128378 posted

    Hi,

    use  this Method

     private void ArrayAddtion()
            {
                int[] arr = new int[] { 1, 2, 3 };
                int sum = 0;
                sum = arr.Sum();
                Response.Write("sum= " + sum);
            }

    and call this method in your button click event like this

     private void button1_Click(object sender, EventArgs e)
            {
                ArrayAddtion();
            }

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 29, 2015 11:49 AM
  • User-256025523 posted

    Thanks Murali !! Let me check this.

    Tuesday, December 29, 2015 12:00 PM
  • User-986267747 posted

    Hi pr_nimbalkar,

    I have array with 10,000 records with numbers. I have to add all the numbers using C#. Which is the fastest way to perform this operation?

    In my experience, if you are use .Net 3.5, you could use the following code:

    int sum = arr.Sum();
    Console.WriteLine(sum);

    If you're not using .NET 3.5 you could do this:

    int sum = 0;
    Array.ForEach(arr, delegate(int i) { sum += i; });
    Console.WriteLine(sum);

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 30, 2015 5:35 AM
  • User-256025523 posted

    Thanks !!

    Wednesday, December 30, 2015 2:29 PM
  • User303363814 posted

    fastest way to perform this operation?

    http://ericlippert.com/2012/12/17/performance-rant/

    Wednesday, December 30, 2015 9:42 PM
  • User-434868552 posted

    @pr_nimbalkar

    to know what is faster, one must time the code.

    based only on my limited testing today, on one million operations, here are my results, fastest to slowest:

    1. c.  33":  Array.ForEach(TenK, delegate (int i) { total += i; });
    2. c.  40":  for (int index = 0; index < 10000; index++) total += TenK[index];
    3. c.  54":  total = TenK.Sum();

    The code snippets, shown below, were run in LINQPad 5 on Windows 8.1 intel core i7 16GB RAM

    N.B.:  there may be faster methods.

    void Main()   // fastest
    {
        Int32[] TenK = new Int32[10000];
        for (int index = 0; index < 10000; index++) TenK[index] = index + 1;
        Console.WriteLine((10000 * 10001) / 2); // compute sum of 1, 2, ..., 9999, 10000 using shortcut
        Int32 loops = 1000000;
        DateTime start = DateTime.Now;
        Int32 total = 0;
        for (int outerIndex = 0; outerIndex < loops; outerIndex++)
        {
            total = 0; // re-initialize because we're using +=
            Array.ForEach(TenK, delegate (int i) { total += i; });
        }
        DateTime end = DateTime.Now;
        Console.WriteLine(end - start);
        Console.WriteLine(total);
    }
    void Main()  // second fastest
    {
        Int32[] TenK = new Int32[10000];
        for (int index = 0; index < 10000; index++) TenK[index] = index + 1;
        Console.WriteLine((10000*10001)/2); // compute sum of 1, 2, ..., 9999, 10000 using shortcut
        Int32    loops = 1000000;
        DateTime start = DateTime.Now;
        Int32 total = 0;
        for (int outerIndex = 0; outerIndex < loops; outerIndex++)
        {
            total = 0;   // re-initialize because we're using +=
            for (int index = 0; index < 10000; index++) total += TenK[index];
        }
        DateTime end   = DateTime.Now;
        Console.WriteLine(end - start);
        Console.WriteLine(total);
    }

    EDIT:

    void Main()   // slowest of these 3 examples occurs when using .Sum() method
    {
        Int32[] TenK = new Int32[10000];
        for (int index = 0; index < 10000; index++) TenK[index] = index + 1;
        Console.WriteLine((10000 * 10001) / 2); // compute sum of 1, 2, ..., 9999, 10000 using shortcut
        Int32 loops = 1000000;
        DateTime start = DateTime.Now;
        Int32 total = 0;
        for (int outerIndex = 0; outerIndex < loops; outerIndex++)
            total = TenK.Sum();
        DateTime end = DateTime.Now;
        Console.WriteLine(end - start);
        Console.WriteLine(total);
    }

    purpose of edit:  to fix copy and paste error for the last example.  mea culpa

    END EDIT.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 30, 2015 10:10 PM
  • User303363814 posted

    If you change the statement

    TenK[index] = index + 1;

    to

    TenK[index] = index * 1000 + 1;

    Then the answer printed for 'total' is incorrect.

    The fastest code which gives the wrong answer is probably

    Console.WriteLine(1);

    But why ask random people on the internet?  Write the code yourself.  Check the answer is correct.  Time it.

    Wednesday, December 30, 2015 11:57 PM