refine 'likely' test.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
wangjiacai 2023-02-12 00:15:06 +08:00
parent e5110406ec
commit 5f339eaa55

View File

@ -1,85 +1,93 @@
#include <iostream> #include <iostream>
#include <thread>
#include <chrono>
#include <sys/time.h> #include <sys/time.h>
#include <limits.h>
#define likely(x) __builtin_expect(!!(x), 1) #define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0) #define unlikely(x) __builtin_expect(!!(x), 0)
using namespace std; using namespace std;
#define SIZE 500000000 const int SIZE = 128 * 1024 * 1024;
const int RANGE = RAND_MAX / 2;
int arr[SIZE]; int arr[SIZE];
void func(int *pval) void func1(int *pval)
{ {
double ret = 0; int big = 0, small = 0;
timeval start, end; timeval start, end;
gettimeofday(&start, nullptr); gettimeofday(&start, nullptr);
while (*pval) for (int idx = 0; idx < SIZE; idx++)
{ {
if (((*pval) == 256)) if (((pval[idx]) > RANGE / 2))
ret = ret + 1234; big++;
else else
ret = ret + 5678; small++;
pval++;
} }
gettimeofday(&end, nullptr); gettimeofday(&end, nullptr);
double usec = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000; double usec = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000;
cout << __func__ << " duration: " << usec << ", ret: " << ret << endl; cout << __func__ << " duration: " << usec << ", big: " << big << ", small:" << small << endl;
}
void func2(int *pval)
{
int big = 0, small = 0;
timeval start, end;
gettimeofday(&start, nullptr);
for (int idx = 0; idx < SIZE; idx++)
{
if (((pval[idx]) <= RANGE / 2))
small++;
else
big++;
}
gettimeofday(&end, nullptr);
double usec = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000;
cout << __func__ << " duration: " << usec << ", big: " << big << ", small:" << small << endl;
} }
void func_likely(int *pval) void func_likely(int *pval)
{ {
double ret = 0; int big = 0, small = 0;
timeval start, end; timeval start, end;
gettimeofday(&start, nullptr); gettimeofday(&start, nullptr);
while (*pval) for (int idx = 0; idx < SIZE; idx++)
{ {
if (likely((*pval) == 256)) if (likely((pval[idx]) > RANGE / 2))
ret = ret + 1234; big++;
else else
ret = ret + 5678; small++;
pval++;
} }
gettimeofday(&end, nullptr); gettimeofday(&end, nullptr);
double usec = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000; double usec = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000;
cout << __func__ << " duration: " << usec << ", ret: " << ret << endl; cout << __func__ << " duration: " << usec << ", big: " << big << ", small:" << small << endl;
} }
void func_unlikely(int *pval) void func_unlikely(int *pval)
{ {
double ret = 0; int big = 0, small = 0;
timeval start, end; timeval start, end;
gettimeofday(&start, nullptr); gettimeofday(&start, nullptr);
while (*pval) for (int idx = 0; idx < SIZE; idx++)
{ {
if (unlikely((*pval) == 256)) if (unlikely((pval[idx]) > RANGE / 2))
ret = ret + 1234; big++;
else else
ret = ret + 5678; small++;
pval++;
} }
gettimeofday(&end, nullptr); gettimeofday(&end, nullptr);
double usec = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000; double usec = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000;
cout << __func__ << " duration: " << usec << ", ret: " << ret << endl; cout << __func__ << " duration: " << usec << ", big: " << big << ", small:" << small << endl;
} }
int main() int main()
{ {
srand(time(nullptr));
for (int i = 0; i < SIZE; i++) for (int i = 0; i < SIZE; i++)
{ {
if (i % 1000 == 0) arr[i] = (rand() % (RANGE + 1)) + 0.499 * RANGE;
*(arr + i) = 256;
else
*(arr + i) = 128;
} }
arr[SIZE - 1] = 0; func1(arr);
std::thread t1(func, arr); func2(arr);
std::thread t2(func_likely, arr); func_likely(arr);
std::thread t3(func_unlikely, arr); func_unlikely(arr);
t1.join();
t2.join();
t3.join();
} }