#include <algorithm>
#include <iostream>
#include <ctime>
#define NON_ZERO
//#define ZERO
#ifdef NON_ZERO
#define FUNCTION1 nonZero1
#define FUNCTION2 nonZero2
#define FILL 0
#endif
#ifdef ZERO
#define FUNCTION1 zero1
#define FUNCTION2 zero2
#define FILL 2000
#endif
bool nonZero1(const int integer);
bool nonZero2(const int integer);
bool zero1(const int integer);
bool zero2(const int integer);
unsigned int test1(const unsigned int size, const int* const buffer);
unsigned int test2(const unsigned int size, const int* const buffer);
void benchmark1(const unsigned int size, const int* const buffer);
void benchmark2(const unsigned int size, const int* const buffer);
void benchmark1(const unsigned int size, const int* const buffer)
{
unsigned long long result = 0;
const std::clock_t startClocks = std::clock();
for (unsigned int i = 0; i < 10; ++i)
{
result += test1(size, buffer);
}
const std::clock_t clocksTaken = std::clock() - startClocks;
std::cout << "Benchmark1. Result: " << result << ". Clocks taken: " << clocksTaken << "." << std::endl;
}
void benchmark2(const unsigned int size, const int* const buffer)
{
unsigned long long result = 0;
const std::clock_t startClocks = std::clock();
for (unsigned int i = 0; i < 10; ++i)
{
result += test2(size, buffer);
}
const std::clock_t clocksTaken = std::clock() - startClocks;
std::cout << "Benchmark2. Result: " << result << ". Clocks taken: " << clocksTaken << "." << std::endl;
}
int main(void)
{
const unsigned int size = 200000000;
int* buffer = new int[size];
std::fill(buffer, buffer + size, FILL);
for (unsigned int i = 0; i < 4; i++)
{
benchmark1(size, buffer);
benchmark2(size, buffer);
std::cout << std::endl;
}
delete[] buffer; buffer = nullptr;
std::cin.sync();
std::cin.get();
return EXIT_SUCCESS;
}
bool nonZero1(const int integer)
{
bool notZero1 = integer != 0;
return notZero1;
}
bool nonZero2(const int integer)
{
bool notZero2 = integer < 0 || integer > 0;
return notZero2;
}
bool zero1(const int integer)
{
bool zero1 = integer == 0;
return zero1;
}
bool zero2(const int integer)
{
bool zero2 = !(integer < 0 || integer > 0);
return zero2;
}
unsigned int test1(const unsigned int size, const int* const buffer)
{
unsigned int result = 0;
for (unsigned int i = 0; i < size; ++i)
{
if (FUNCTION1(buffer[i]))
{
++result;
}
}
return result;
}
unsigned int test2(const unsigned int size, const int* const buffer)
{
unsigned int result = 0;
for (unsigned int i = 0; i < size; ++i)
{
if (FUNCTION2(buffer[i]))
{
++result;
}
}
return result;
}