#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 |