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;
}
}
}
运行结果如下: