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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:33:36+00:00 2026-05-16T18:33:36+00:00

I have the following C# code fragment: using System; class count { public static

  • 0

I have the following C# code fragment:

using System;

class count {
 public static void Main()
 {
  int [] a = {-30, 30, -20, -10, 40, 0, 10, 5};
  int i,j,k;
  int N=8;

  for (i=0; i < N; ++i)
   for (j=i+1; j < N; ++j)
    for (k=j+1; k < N; ++k)
     if (a[i] + a[j] + a[k] == 30)
      Console.WriteLine (a[i].ToString () + a[j].ToString() + a[k].ToString());

 }
}

What the above program does is, find out the triplets a1, a2, a3 from an Array A, such that the sum of the triplets is 30.

I want to know how I can do the sum calculation using the C# Parallel.For extension.

I know that this is used as an interview question and there are better alternative algorithms than an i,j,k loop. However, all I want is just to understand how to perform this using Parallel extensions for C# , in an effective way.

  • 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-16T18:33:36+00:00Added an answer on May 16, 2026 at 6:33 pm

    dan’s answer will work and is a correct way to use Parallel.For, but I went through the trouble of profiling the code and I think you’ll find that parallelising this won’t make it any faster. Each Parellel.For makes several new threads, usually more than three, so with 3 nested Parellel.Fors you will have have at least 3^3 (27) threads, which is more way more than the number of logical processors on any machine. The extra threads will if anything add an overhead and slow it down.

    So why not just have one Parallel.For and 2 ordinary for loops – this will mean that there are around 3-4 threads which will work great on a dual or quad core machine. Like this method:

    static void Test2(int[] a)
    {
        int N = a.Length;
        int total = 0;
        Object locker = new object();
    
        Parallel.For(0, N, i => 
        {
           for (int j = i + 1; j < N; ++j)
                for (int k = j + 1; k < N; ++k)
                    if (a[i] + a[j] + a[k] == 30)
                        lock(locker)
                            total++; 
        });
    }
    

    Take the following code used to profile both methods:

    class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            int[] arr = new int[100];
            arr = arr.Select(i => r.Next(-30, 30)).ToArray();            
    
            Profile(Test0, arr, 20);
            Profile(Test1, arr, 20);
            Profile(Test2, arr, 20);
    
            Console.WriteLine("Test0: {0} ms", Profile(Test0, arr, 100).TotalMilliseconds);
            Console.WriteLine("Test1: {0} ms", Profile(Test1, arr, 100).TotalMilliseconds);
            Console.WriteLine("Test2: {0} ms", Profile(Test2, arr, 100).TotalMilliseconds);
    
            Console.ReadLine();
        }
    
        static void Test0(int[] a)
        {
            int N = a.Length;
            int total = 0;
    
            for (int i = 0; i < N; ++i)
                for (int j = i + 1; j < N; ++j)
                    for (int k = j + 1; k < N; ++k)
                        if (a[i] + a[j] + a[k] == 30)
                            total++;
        }
    
        static void Test1(int[] a)
        {
            int N = a.Length;
            int total = 0;
            Object locker = new object();
    
            Parallel.For(0, N, i => Parallel.For(i+1, N, j => Parallel.For(j+1, N, k =>
            {
                if (a[i] + a[j] + a[k] == 30)
                    lock(locker)
                        total++;
            })));
        }
    
        static void Test2(int[] a)
        {
            int N = a.Length;
            int total = 0;
            Object locker = new object();
    
            Parallel.For(0, N, i => 
            {
                for (int j = i + 1; j < N; ++j)
                    for (int k = j + 1; k < N; ++k)
                        if (a[i] + a[j] + a[k] == 30)
                            lock(locker) 
                                total++;
            });
        }
    
        static TimeSpan Profile<T>(Action<T> action, T param, int repeats)
        {
            Stopwatch s = new Stopwatch();
    
            for (int i = 0; i < repeats; i++)
            {
                s.Start();
                action(param);
                s.Stop();
            }
    
            return new TimeSpan(s.ElapsedTicks/repeats);
        }
    }
    

    This yields these results for average execution time for each method: (in Release mode, using .Net 4.0, on a quad core Intel Core i5 machine):

    Test0: 0.2544 ms
    Test1: 3.3433 ms
    Test2: 0.1391 ms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That callback is removing the classes from the previously active… May 17, 2026 at 2:11 am
  • Editorial Team
    Editorial Team added an answer Your question makes little sense. SSL is a secure transport… May 17, 2026 at 2:11 am
  • Editorial Team
    Editorial Team added an answer By not specifying a style parameter to the convert function… May 17, 2026 at 2:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following code fragment #include <iostream> #include <ostream> #include <vector> using namespace
I have the following java code fragment while (condition1){ switch (someinteger){ case 1: if(condition2)
I have using the following code for developing tabs. $(document).ready(function() { $('#container-1').tabs(); } ....
I have following code <div class=img> <img src=image/1.jpg class=img_s style=display:none; /> <img src=image/2.jpg class=img_s
i have following code for allocation two dimensional array #include <iostream> using namespace std;
I'm resizing jpegs by using the Graphics.DrawImage method (see code fragment below). Can anyone
I have the need of a click tab event. I am using the following
I have following code: $telnums = array(10, 20, 30); $obj = new StdClass(); $obj->telnums
I am using jQuery UI Tabs . <div id=tabs> <ul id=tablist> <li><a href=#fragment-1><span>One</span></a></li> </ul>
I have the following input <row test=1 /> and want to generate the following

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.