Last active
June 1, 2020 05:23
-
-
Save skywind3000/3db446c9b0cf64148445dc4cec8c1d5e to your computer and use it in GitHub Desktop.
branch.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <time.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <chrono> | |
#include <thread> | |
int64_t clock_realtime() | |
{ | |
return std::chrono::duration_cast<std::chrono::nanoseconds>( | |
std::chrono::system_clock::now().time_since_epoch()).count(); | |
} | |
void isleep(unsigned long millisecond) | |
{ | |
std::this_thread::sleep_for(std::chrono::milliseconds(millisecond)); | |
} | |
#define TIMES 1000 | |
#define N 100000 | |
int min = 3333, max = 6666; | |
void benchmark1() | |
{ | |
isleep(1000); | |
srand(1); | |
int64_t ts = clock_realtime(); | |
int s = 0; | |
for (int k = 0; k < TIMES; k++) { | |
for (int j = 0; j < N; j++) { | |
int x = rand() % 10000; | |
if (x >= min && x <= max) { | |
s += x; | |
} | |
else { | |
s -= x; | |
} | |
} | |
} | |
ts = clock_realtime() - ts; | |
printf("bench 1: time=%d s=%d\n", (int)ts, s); | |
} | |
void benchmark2() | |
{ | |
isleep(1000); | |
srand(1); | |
int64_t ts = clock_realtime(); | |
int s = 0; | |
for (int k = 0; k < TIMES; k++) { | |
for (int j = 0; j < N; j++) { | |
int x = rand() % 10000; | |
if (((x - min) | (max - x)) >= 0) { | |
s += x; | |
} | |
else { | |
s -= x; | |
} | |
} | |
} | |
ts = clock_realtime() - ts; | |
printf("bench 2: time=%d s=%d\n", (int)ts, s); | |
} | |
int main(void) | |
{ | |
benchmark1(); | |
benchmark2(); | |
return 0; | |
} | |
/* | |
bench 1: time=2063884 s=-1103064781 | |
bench 2: time=1316591 s=-1103064781 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment