【问题标题】:code optimization histogram c++ from matlab来自matlab的代码优化直方图c ++
【发布时间】:2017-05-11 20:30:08
【问题描述】:
LIBIQTOOL_API void Hist(std::vector<double>input, std::vector<double> bins, std::vector<double>& histogram)
{
    double minY = *std::min_element(std::begin(input), std::end(input));
    double maxY = *std::max_element(std::begin(input), std::end(input));
    std::vector<double> edges;    
    edges.push_back(-1 * std::numeric_limits<double>::infinity());
    for (int i = 0; i < bins.size() - 1; i++)
    {
        edges.push_back(bins[i] + 0.0100 / 2);
    }
    edges.push_back(std::numeric_limits<double>::infinity());


    //histC

    histogram.resize(edges.size() - 1);
#pragma omp parallel for
    for (int i = 0; i < input.size(); i++)
    {
        for (int j = 0; j < edges.size() - 1; j++)
        {
            if ((edges[j] < input[i]) && (input[i] <= edges[j + 1]))
            {
                histogram[j] = histogram[j] + 1;
                break;
            }
        }
    }

    histogram[histogram.size() - 1] = histogram[histogram.size() - 1] + histogram[histogram.size() - 2];
    histogram.pop_back();
}

输入向量的大小为 3,000,000++,bin 的数量约为 7000。
我采用了 Matlab 的 Hist() 函数并在 c++ 中创建了我需要的代码。
但是运行需要很长时间,您能看到更多可以在这里完成的运行时优化吗?
我做到了:
一种。当您找到放置当前号码的垃圾箱时打破
湾。使用 openMP

【问题讨论】:

    标签: c++ matlab optimization


    【解决方案1】:

    可能的优化:

    • 不要通过值传递输入数据,而是通过 const 引用传递
    • 在对正确的 bin 进行线性搜索时,不检查下限,只检查每个 bin 的上限。
    • 或者:由于您的 bin 是单调排序的并且没有间隙,因此对正确的 bin 执行二进制搜索,而不是线性搜索。

    最后一个应该会给你最大的收益,其他的实现起来更简单。

    顺便说一句,你填充边缘向量的方式看起来很奇怪。

    【讨论】:

    • 难道没有办法不经过任何搜索就可以获取 bin 的索引吗?
    • 只有当你可以找到一个分析函数来匹配值到 bin 时,例如如果所有 bin 具有相同的宽度。
    • 它们是相同的width.-INF 0.00500000000000000 0.0150000000000000 0.0250000000000000 0.0350000000000000 0.0450000000000000 0.0550000000000000 0.0650000000000000 0.0750000000000000 0.0850000000000000 0.0950000000000000 0.105000000000000 0.115000000000000 0.125000000000000 0.135000000000000 0.145000000000000 0.155000000000000等,,, 跨度>
    • 好吧,那就是 bin_index = floor((value - lower_bound) / bin_width)。使用一些代码来检查 inf/-inf 是否包含。
    • int index = floor((input[i] - (0.0100 / 2)) / 0.01); ??
    猜你喜欢
    • 2011-11-30
    • 1970-01-01
    • 2015-06-23
    • 2014-12-31
    • 2011-11-03
    • 2013-11-12
    • 1970-01-01
    • 2015-02-21
    • 2016-10-21
    相关资源
    最近更新 更多