Win32API、MFC、.NET并行求和运算(星星笔记)

1.运用Win32API实现并行求和运算,程序如下:


// Win32APIadd.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include "time.h"

HANDLE finish[2];
HANDLE finish2;
long long sum[2];
DWORD WINAPI ThreadOne(LPVOID param){
	for (long i =1;i<= 500000000;i++)
	{
		sum[0] +=i;
	}
	SetEvent(finish[0]);
	return 0;
}

DWORD WINAPI ThreadTwo(LPVOID param){
	for (long i =500000001;i<= 1000000000;i++)
	{
		sum[1] +=i;
	}
	SetEvent(finish[1]);
	return 0;
}

DWORD WINAPI ThreadThree(LPVOID param){
	long long sumserial = 0;
	for (long i =1;i<= 1000000000;i++)
	{
		sumserial +=i;
	}
	printf("sumserial=%lld\n",sumserial);
	SetEvent(finish2);
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	clock_t start = clock();
	long long sumand = 0;
	finish[0] = CreateEvent(NULL ,false,false,NULL);
	finish[1] = CreateEvent(NULL ,false,false,NULL);
	finish2 = CreateEvent(NULL ,false,false,NULL);
	HANDLE thread1 = CreateThread(NULL,0,ThreadOne,NULL,0,NULL);
	HANDLE thread2 = CreateThread(NULL,0,ThreadTwo,NULL,0,NULL);
	WaitForMultipleObjects(2,finish,true,INFINITE);
	for (int i=0;i<2;i++)
	{
		sumand +=sum[i];
	}
	clock_t end = clock();
	printf("sumand=%lld\n",sumand);
	printf("andtime=%d\n",end - start);

	clock_t start2 = clock();
	HANDLE thread3 = CreateThread(NULL,0,ThreadThree,NULL,0,NULL);
	WaitForSingleObject(finish2,INFINITE);
	clock_t end2 = clock();
	printf("serialtime=%d\n",end2 - start2);
	system("pause");
	return 0;
}
运行结果如下:

2.运用MFC实现并行求和,程序如下:

// MFC_A_ADD.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <afxmt.h>
#include <iostream>
#include <afxwin.h>
#include "time.h"
using namespace std;

long long sum[2] = {0,0} ;

CEvent faxEvent1(false); 
CEvent faxEvent2(false);
CEvent faxEvent(false);

UINT threadProc4(LPVOID param){
	long long temp = 0;
	for(long i=1;i<=500000000;i++){
		temp  += i;
	}
	sum[0] = temp;
	SetEvent(faxEvent1);
	return 0;
}

UINT threadProc5(LPVOID param){
	long long temp = 0;
	for(long i=500000001;i<=1000000000;i++){
		temp += i;
	}
	sum[1] = temp;
	SetEvent(faxEvent2);
	return 0;
}
UINT threadProc6(LPVOID param){

	long long sum = 0;

	for(long i=1;i<=1000000000;i++){
		sum +=i;
	}
	printf("sumserial=%lld\n",sum);
	SetEvent(faxEvent);
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	long long sumand = 0;
	clock_t start = clock();
	AfxBeginThread(threadProc4,NULL);
	AfxBeginThread(threadProc5,NULL);
	WaitForSingleObject(faxEvent1,INFINITE);
	WaitForSingleObject(faxEvent2,INFINITE);

	for (int i=0;i<2;i++)
	{
		sumand+=sum[i];
	}
	clock_t end = clock();
	printf("sumand=%lld\n",sumand);
	printf("paralleltime=%d\n",end - start);

	clock_t start2 = clock();
	AfxBeginThread(threadProc6,NULL);
	WaitForSingleObject(faxEvent,INFINITE);
	clock_t end2 = clock();
	printf("serialtime=%d\n",end2 - start2);
	system("pause");

	return 0;
}
运行结果如下:

3.运用.NET实现并行求和,程序如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace NETadd
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            Work work1 = new Work(1);
            ThreadStart thread1 = new ThreadStart(work1.pSumto);
            Thread newthread1 = new Thread(thread1);
            Work work2 = new Work(2);
            ThreadStart thread2 = new ThreadStart(work2.pSumto);
            Thread newthread2 = new Thread(thread2);

            stopwatch.Start();
            newthread1.Start();
            newthread2.Start();
            newthread1.Join();
            newthread2.Join();
            stopwatch.Stop();

            TimeSpan timeSpan = stopwatch.Elapsed;
            double milliseconds = timeSpan.TotalMilliseconds;
            Console.Write("parallel sum = {0}\n",(work1.getSum()+work2.getSum()));
            Console.Write("parallel time =");
            Console.Write(milliseconds);

            stopwatch.Start();
            Console.Write("\nserial sum = {0}\n", new Work(1).sumto());
            stopwatch.Stop();
            TimeSpan timeSpan2 = stopwatch.Elapsed;
            double milliseconds2 = timeSpan2.TotalMilliseconds;
            Console.Write("serial time =");
            Console.Write(milliseconds2);
            Console.Read();
        }
    }

    class Work
    {
        private long sum = 0;
        private long start;
        public Work(long i)
        {
            this.start = i;
        }
        public void pSumto()
        {
            for (long i = start; i <= 1000000000; i += 2)
            {
                sum += i;
            }
        }
        public long sumto()
        {
            long sumto = 0;
            for (long i = start; i <= 1000000000; i++)
            {
                sumto += i;
            }
            return sumto;
        }
        public long getSum()
        {
            return sum;
        }
    }

}
运行结果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值