简体   繁体   中英

How can I divide one for loop to several loops and run them with threads to get shorter run time?

I am new to C# multi-threading . I have defined a simple for loop that when I press a button it starts counting from 0 to 10000 and using a stopwatch I captured the time period that it takes to finish this loop.

private void button2_Click(object sender, EventArgs e)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (double i = 0; i < 5000; i++)
        {
            Console.WriteLine(i.ToString() + ", ");

        }
        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);
        MessageBox.Show(elapsedTime);
    }

On my laptop it takes: 15':28''

On the other hand, I tried multi-threading in order to see whether I can shorten this time or not. What I did was:

1) Defining 5 functions (Func1,Func2, ...., Func5)

2) Inside each function I defined a for loop that counts one fifth of the main for loop:

Func1: 0 - 1000

Func2: 1000 - 2000

Func3: 2000 - 3000

Func4: 3000 - 4000

Func5: 4000 - 4000

3) Finally I defined five different threads and called each function in each of the threads.

Logically I expected to get the result in a shorter time, but the run time was the same as the time that I got without threads.

private void button1_Click(object sender, EventArgs e)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();

        Thread T1 = new Thread(new ThreadStart(Func1));
        T1.Start();

        Thread T2 = new Thread(new ThreadStart(Func2));
        T2.Start();

        Thread T3 = new Thread(new ThreadStart(Func3));
        T3.Start();

        Thread T4 = new Thread(new ThreadStart(Func4));
        T4.Start();

        Thread T5 = new Thread(new ThreadStart(Func5));
        T5.Start();

        T1.Join();
        T2.Join();
        T3.Join();
        T4.Join();
        T5.Join();
        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);
        MessageBox.Show(elapsedTime);
    }

Can I shorten the run time using threads? If yes, how? and if no, what method should I use?

I tested this code to see the functionality of C# threads, It may seem useless but I will use it in an image processing app to shorten the run time of several for loops.

Please help.

You have a restaurant. You only have one cook that cooks great but he's darn slow. You have one waiter that takes all the orders and hands them down to the cook. This is too slow and customers are starting to complain.

You have a bright idea (or not). You hire 100 waiters, that should make things much faster! Ok, now you've got orders getting to the cook a whole lot faster, but the food is still coming out at the same speed. How's that possible?

Change cook for Console.WriteLine and waiters for Threads and thats essentially what you are trying to do. You see how pointless it is?

Now, a whole different story would be if you have 10 cooks (processors) and only one waiter (thread). In that case, hiring 9 more waiters would make perfect sense.

Moral of the story? Use more threads and parallelize when you have processor bound tasks to perform and you have processors waiting around idly. Otherwise, there is normally better ways to do things than spinning up useless threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM