码迷,mamicode.com
首页 > 编程语言 > 详细

选择排序的openMP实现

时间:2015-10-22 13:52:54      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

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

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include "omp.h"
#include <iostream>
using namespace std;
void swap(long *a, long *b); //交换两个数
int _tmain(int argc, _TCHAR* argv[])
{

    omp_set_num_threads(2);
    long  str[10000];
    long     i;
    clock_t t1 = clock();
    //初始化数组
#pragma omp parallel for
    for (i = 0; i < 10000; i++)
    {
        str[i] = 10000 - i;
    }


#pragma omp parallel for
    for (i = 0; i < 10000; i++)
    {
        for (long j = i + 1; j < 10000; j++)
        {
            if (str[i] > str[j])
            {
                swap(&str[i], &str[j]);
            }
        }
    }

    clock_t t2 = clock();

    //for (i = 0; i < 10000; i++)
    //printf("%d\n", str[i]);

    clock_t t3 = t2-t1;

    printf("parallel time =%d\n",t3);

    long  str2[10000];
    long     m;
    t1 = clock();
    for (m = 0; m < 10000; m++)
    {
        str[m] = 10000 - m;
    }



    for (m = 0; m < 10000; m++)
    {
        for (long n = m + 1; n < 10000; n++)
        {
            if (str[m] > str[n])
            {
                swap(&str[m], &str[n]);
            }
        }
    }

    t2 = clock();

    //for (m = 0; m < 10000; m++)
    //printf("%d\n", str[m]);
    clock_t t4 = t2-t1;
    
    printf("serial time=%d\n",t4);

    printf("加速比:%f\n",(t4*1.0/t3));

    system("Pause");
    return 0;
}

void swap(long *a, long *b)
{
    long     c;
    c = *a;
    *a = *b;
    *b =  c;
}

 

选择排序的openMP实现

标签:

原文地址:http://www.cnblogs.com/little-aladdin/p/4900496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!