zoukankan      html  css  js  c++  java
  • vectorized case branch

    #include <utility>
    #include <vector>
    #include <list>
    #include <algorithm>
    #include <iostream>
    #include <functional>
    #include "cmath"
    
    #define likely(x)       __builtin_expect(!!(x), 1)
    #define unlikely(x)     __builtin_expect(!!(x), 0)
    
    int test_vec(int M, const std::vector<int>& vec) {
        auto start = clock();
        uint32_t sum = 0;
        for (int i = 0; i < M; ++i) {
            for (auto v : vec) {
                if (likely(v < 10)) {
                    sum += v;
                } else {
                    sum += v * 2;
                }
            }
        }
        auto end = clock();
        std::cout << "cost:" << (double)(end - start) / CLOCKS_PER_SEC << "s" << "
    ";
        return sum;
    }
    
    
    
    int main() {
        int N = 10000;
        std::vector<int> vec;
        for (int i = 0; i < N; ++i) {
            vec.emplace_back(rand() % 7);
        }
        int M = 100000;
        auto sum = test_vec(M, vec);
        return sum;
    }
    
    cost:1.42718s
    
    sum += v < 10 ? v : 2 * v;
    
    no-tree-vectorize none (SSE) SSE4.2 AVX2 AVX512f native
    cost:1.00838s cost:0.347205s cost:0.283971s cost:0.189798s cost:0.080189s cost:0.126796s
  • 相关阅读:
    VMware6.0-vCenter的安装准备及安装
    VeeamBackup9.5安装与配置
    VeeamOne(Free Edition 9.5 )-安装与配置
    UIDatePicker
    UIImagePicker照片选择器
    UIImageView
    UILabel
    UIScrollView 期本使用方法
    UISegment
    UISlide
  • 原文地址:https://www.cnblogs.com/stdpain/p/15163398.html
Copyright © 2011-2022 走看看