// OpenMP1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include"omp.h" #include<Windows.h> #include<time.h> #include<iostream> using namespace std; #define NUM_THREADS 4 int _tmain(int argc, _TCHAR* argv[]) { omp_set_num_threads(NUM_THREADS); long long sum=0; clock_t time_begin =clock(); #pragma omp parallel for reduction(+:sum) for(long i=1;i<=1000000000;i++) { sum+=i; } clock_t time_end=clock(); double s1=time_end-time_begin; cout<<"Sum="<<sum<<endl; cout<<"并行时间="<<s1<<endl; //****************************************************** sum=0; time_begin=clock(); for(long i=1;i<=1000000000;i++) { sum+=i; } time_end=clock(); double s2=time_end-time_begin; cout<<"Sum="<<sum<<endl; cout<<"串行时间="<<s2<<endl; cout<<"加速比为"<<(s2/s1)<<endl; system("pause"); return 0; }
时间: 2024-10-22 19:47:48