c++ openMP 循环处理数组 耗时对比

//在图像处理中,计算很耗时,考虑用openMP并行化,

//本文对比不同办法的循环处理数组的耗费时间,在visual studio 2017调试通过。

//实际测试对比,简单循环并行化、分块技术、编译制导的效果都比较显著。

//但是使用分块技术,两重循环对应行数和列数,比较直观,应用价值大

#include <iostream>
#include <windows.h>
#include <thread>
#include <omp.h>
#include "time.h"
const int  width = 10000;
const int height = 10000;
long   array1[width * height];
long  array2[width * height];
long  array3[width * height];
int func1(int w, int h)//不使用并行化
{
    clock_t t1 = clock();
    long t = w * h;
    for (long i = 0; i < t; i++)//遍历每个元素
    {
        array1[i] = 0; array2[i] = 0;
        array3[i] = array1[i] + array2[i];
    }
    clock_t t2 = clock();
    std::cout << t2 - t1 << std::endl;
    std::cout << "---" << std::endl;
    return 0;
}

int func2(int w, int h)//简单循环并行化
{
    clock_t t1 = clock();
    long t = w * h;
#pragma omp parallel for
    for (long i = 0; i < t; i++)
    {
        array1[i] = 0; array2[i] = 0;
        array3[i] = array1[i] + array2[i];
    }
    clock_t t2 = clock();
    std::cout << t2 - t1 << std::endl;
    std::cout << "---" << std::endl;
    return 0;
}
int func3(int w, int h)//使用分块技术将循环转换为等效的多线程
{
    clock_t t1 = clock();
    int i; int j; int m; int n;
#pragma omp parallel for private(i,j)
    for (i = 0; i < h; i++)//遍历行
    {
        for (j = i * w; j < i*w + w; j++)//遍历列
        {
            array1[j] = 0; array2[j] = 0;
            array3[j] = array1[j] + array2[j];
        }

    }
    clock_t t2 = clock();
    std::cout << t2 - t1 << std::endl;
    std::cout << "---" << std::endl;
    return 0;
}

int func3p(long srcArry1[], long srcArry2[], int w, int h, long destsrcArry[])

///数组做参数,使用分块技术将循环转换为等效的多线程
{
    clock_t t1 = clock();
    int i; int j; int m; int n;
#pragma omp parallel for private(i,j)
    for (i = 0; i < h; i++)//遍历行
    {
        for (j = i * w; j < i * w + w; j++)//遍历列
        {
            srcArry1[j] = 0; srcArry2[j] = 0;
            destsrcArry[j] = srcArry1[j] + srcArry1[j];
        }

    }
    clock_t t2 = clock();
    std::cout << t2 - t1 << std::endl;
    std::cout << "---" << std::endl;
    return 0;
}

int func4(int w, int h)//使用编译制导将循环转换成等效的多线程度
{
    clock_t t1 = clock();
    long t = w * h;
    long i;
#pragma omp parallel sections private (i)
    {
#pragma omp section
        {
            for (i = 0; i < t / 2; i++)
            {
                array1[i] = 0; array2[i] = 0;
                array3[i] = array1[i] + array2[i];
            }
        }
#pragma omp section
        {  for (i = t / 2 + 1; i < t; i++)
        {
            array1[i] = 0; array2[i] = 0;
            array3[i] = array1[i] + array2[i];
        }
        }

    }
    clock_t t2 = clock();
    std::cout << t2 - t1 << std::endl;
    std::cout << "---" << std::endl;
    return 0;
}


int main()
{
    func1(width, height);
    func2(width, height);
    func3(width, height);

    func3p(array1, array2, width, height, array3);
    func4(width, height);
    
    system("pause");
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值