• 周四. 5月 30th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

vectorized case branch

admin

11月 28, 2021
#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

《vectorized case branch》有一个想法
  1. Wow, awesome blog format! How long have you been blogging for?

    you make running a blog glance easy. The full glance of your site
    is great, let alone the content material!
    You can see similar here e-commerce

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注