This commit is contained in:
parent
e5110406ec
commit
5f339eaa55
@ -1,85 +1,93 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <sys/time.h>
|
||||
#include <limits.h>
|
||||
|
||||
#define likely(x) __builtin_expect(!!(x), 1)
|
||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
||||
|
||||
using namespace std;
|
||||
#define SIZE 500000000
|
||||
const int SIZE = 128 * 1024 * 1024;
|
||||
const int RANGE = RAND_MAX / 2;
|
||||
|
||||
int arr[SIZE];
|
||||
|
||||
void func(int *pval)
|
||||
void func1(int *pval)
|
||||
{
|
||||
double ret = 0;
|
||||
int big = 0, small = 0;
|
||||
timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
while (*pval)
|
||||
for (int idx = 0; idx < SIZE; idx++)
|
||||
{
|
||||
if (((*pval) == 256))
|
||||
ret = ret + 1234;
|
||||
if (((pval[idx]) > RANGE / 2))
|
||||
big++;
|
||||
else
|
||||
ret = ret + 5678;
|
||||
pval++;
|
||||
small++;
|
||||
}
|
||||
gettimeofday(&end, nullptr);
|
||||
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)
|
||||
{
|
||||
double ret = 0;
|
||||
int big = 0, small = 0;
|
||||
timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
while (*pval)
|
||||
for (int idx = 0; idx < SIZE; idx++)
|
||||
{
|
||||
if (likely((*pval) == 256))
|
||||
ret = ret + 1234;
|
||||
if (likely((pval[idx]) > RANGE / 2))
|
||||
big++;
|
||||
else
|
||||
ret = ret + 5678;
|
||||
pval++;
|
||||
small++;
|
||||
}
|
||||
gettimeofday(&end, nullptr);
|
||||
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)
|
||||
{
|
||||
double ret = 0;
|
||||
int big = 0, small = 0;
|
||||
timeval start, end;
|
||||
gettimeofday(&start, nullptr);
|
||||
while (*pval)
|
||||
for (int idx = 0; idx < SIZE; idx++)
|
||||
{
|
||||
if (unlikely((*pval) == 256))
|
||||
ret = ret + 1234;
|
||||
if (unlikely((pval[idx]) > RANGE / 2))
|
||||
big++;
|
||||
else
|
||||
ret = ret + 5678;
|
||||
pval++;
|
||||
small++;
|
||||
}
|
||||
|
||||
gettimeofday(&end, nullptr);
|
||||
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()
|
||||
{
|
||||
srand(time(nullptr));
|
||||
for (int i = 0; i < SIZE; i++)
|
||||
{
|
||||
if (i % 1000 == 0)
|
||||
*(arr + i) = 256;
|
||||
else
|
||||
*(arr + i) = 128;
|
||||
arr[i] = (rand() % (RANGE + 1)) + 0.499 * RANGE;
|
||||
}
|
||||
arr[SIZE - 1] = 0;
|
||||
std::thread t1(func, arr);
|
||||
std::thread t2(func_likely, arr);
|
||||
std::thread t3(func_unlikely, arr);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
func1(arr);
|
||||
func2(arr);
|
||||
func_likely(arr);
|
||||
func_unlikely(arr);
|
||||
}
|
Loading…
Reference in New Issue
Block a user