Win32API、MFC、.NET并行求和运算(星星笔记)
来源:互联网 发布:大数据公司 英文 编辑:程序博客网 时间:2024/05/16 05:54
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
- Win32API、MFC、.NET并行求和运算(星星笔记)
- Win32API、MFC、.NET并行求数值积分PI(星星笔记)
- Win32API、MFC、.NET多线程并行求矩阵乘法(星星笔记)
- java 多线程并行计算之矩阵乘法(星星笔记)
- java 多线程并行计算之求素数(星星笔记)
- OpenMP 之并行区域方法求和(学习笔记)
- OpenMP 之for指令并行求和(学习笔记)
- OpenMP 之 sections 求素数、求和、求积分圆周率(星星笔记)
- java 多线程并行计算之矩阵乘法继承Thread类实现(星星笔记)
- VS2010&.Net 4.0 之并行运算(Parallel)(For、Foreach)
- 【CUDA并行编程之三】Cuda矢量求和运算
- 【CUDA并行编程之三】Cuda矢量求和运算
- 并行运算(平行运算)
- Win32API学习笔记第五章(一)
- Win32API学习笔记第五章(二)
- Win32API学习笔记第五章(三)
- Win32API学习笔记第六章(一)
- Win32API学习笔记第六章(二)
- 工厂模式
- WSDL
- 组合模式
- Program in c that will make the CPU run at 100%
- POJ3253 Fence Repair(贪心)
- Win32API、MFC、.NET并行求和运算(星星笔记)
- Java学习日记07——运算符
- 删除列表控件(List Control)中的所有选中行(多选)
- Java基础-->Java开发前奏
- Kruskal算法
- 构造器,子类中必须要实现的有哪几种情况
- Android与服务器端数据交互(http协议整合struts2+android)
- 树的判定
- CentOS 6.5 下安装 VirtulBox(2014.10.1)