Part 97 Performance of a multithreaded program

class Program
    {
        static void Main(string[] args)
        {
            Stopwatch s = new Stopwatch();
            s.Start();
            EvenNumbersSum();
            OddNumbersSum();
            s.Stop();
            Console.WriteLine("before using multiple threads"+s.ElapsedMilliseconds);
            s = new Stopwatch();
            s.Start();
            Thread t1 = new Thread(EvenNumbersSum);
            Thread t2 = new Thread(OddNumbersSum);
            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
            s.Stop();
            Console.WriteLine("after using multiple threads"+s.ElapsedMilliseconds);

        }

        public static void EvenNumbersSum()
        {
            double sum=0;
            for(int i=0;i<=50000000;i++)
            {
                if(i%2==0)
                {
                    sum += i;
                }
            }
            Console.WriteLine("sum= "+sum);
        }

        public static void OddNumbersSum()
        {
            double sum = 0;
            for (int i = 0; i <= 50000000; i++)
            {
                if (i % 2 == 1)
                {
                    sum += i;
                }
            }
            Console.WriteLine("sum= " + sum);
        }
    }

时间: 2024-10-07 16:42:22

Part 97 Performance of a multithreaded program的相关文章

Part 95 to 96 Deadlock in a multithreaded program

Part 95   Deadlock in a multithreaded program class Program { static void Main(string[] args) { Console.WriteLine("main start"); Account a1 = new Account(001,10000); Account a2 = new Account(002,20000); AccountManager m1 = new AccountManager(a1,

C# for Beginner Part 86 to 97

Part 86   Multithreading in C# What is a Process: Process is what the operatin system uses to facilitate(帮助) the execution of a program by providing the resources required.Each process has unique process Id associated with(关联) it. You can view the pr

[转] Vmware vs Virtualbox vs KVM vs XEN: virtual machines performance comparison

http://www.ilsistemista.net/index.php/virtualization/1-virtual-machines-performance-comparison.html?limitstart=0 Today, "Virtual machine" seems to be a magic words in the computer industry. Why? Simply stated, this technology promise better serv

【转】Multithreaded Python Tutorial with the “Threadworms” Demo

The code for this tutorial can be downloaded here: threadworms.py or from GitHub. This code works with Python 3 or Python 2, and you need Pygame installed as well in order to run it. Click the animated gif to view a larger version. This is a tutorial

Checking the Performance of FindArray

FindArray Example Let's create a program that shows how a sample C++ compiler generates code for a function named FindArray. Later, we will write an assembly language version of the function, attempting to write more efficient code than the C++ compi

Timer.5 - Synchronising handlers in multithreaded programs

This tutorial demonstrates the use of the boost::asio::strand class to synchronise callback handlers in a multithreaded program. The previous four tutorials avoided the issue of handler synchronisation by calling the boost::asio::io_service::run() fu

Measure Java Performance – Sampling or Instrumentation

copy from https://blog.codecentric.de/en/2011/10/measure-java-performance-sampling-or-instrumentation/ In recent discussions, I noticed a confusion about the differences between measuring with Sampling andInstrumentation.I hear about which one should

Optimizing Item Import Performance in Oracle Product Hub/Inventory

APPLIES TO: Oracle Product Hub - Version 12.1.1 to 12.1.1 [Release 12.1] Oracle Inventory Management - Version 12.1.1 to 12.1.1 [Release 12.1] Oracle Item Master - Version 12.0.6 to 12.0.6 [Release 12] Information in this document applies to any plat

C# 内存模型

C# 内存模型 This is the first of a two-part series that will tell the long story of the C# memory model. The first part explains the guarantees the C# memory model makes and shows the code patterns that motivate the guarantees; the second part will detai