Commit 85976ae0 authored by Gluttton's avatar Gluttton

Speed tests were rewritten as benchmarks (instead of unit tests). Submodule…

Speed tests were rewritten as benchmarks (instead of unit tests). Submodule google benchmark was added.
parent 328be50d
[submodule "gmock"]
path = gmock
url = https://github.com/mgoral/googlemock
[submodule "benchmark"]
path = benchmark
url = https://github.com/google/benchmark.git
Subproject commit be84ed037323d5d7fe1a4d22975b615b9414f93f
OPTIMIZATION ?= O0
CXX = g++
INCLUDES = -I. -I../benchmark/include
CXXFLAGS = -c \
-MD \
-std=c++11 -march=native -mtune=native \
-Wall -Wextra \
-$(OPTIMIZATION)
LDPATH = -L../benchmark/lib
LDADD = -lbenchmark -lpthread -lgtest -lgtest_main
SRC = SpeedTest.cpp
OBJ = $(SRC:.cpp=.o)
BENCHMARK = boolinqbenchmakr
TMP_DATA = $(shell find . -type f -name '*~')
all: $(BENCHMARK)
$(BENCHMARK): $(OBJ)
$(CXX) $(OBJ) $(LDPATH) $(LDADD) -o $@
.cpp.o:
$(CXX) $(CXXFLAGS) $(INCLUDES) $< -o $@
clean:
rm -rf $(OBJ) $(BENCHMARK) $(TMP_DATA) $(COV_DATA) $(PRO_DATA)
......@@ -41,7 +41,6 @@ SRC = AllTest.cpp \
SelectRangeTest.cpp \
SkipRangeTest.cpp \
SkipWhileRangeTest.cpp \
SpeedTest.cpp \
SumTest.cpp \
TakeRangeTest.cpp \
TakeWhileRangeTest.cpp \
......
#include <benchmark/benchmark.h>
#include <gtest/gtest.h>
#include <iostream>
#include <stdlib.h>
#include "boolinq.h"
using namespace boolinq;
std::vector<int> vec(1000000, 0);
TEST(SpeedTest1, Init)
constexpr std::size_t benchmarkVectorSize {1000000};
std::vector<int> vecBoolinq (benchmarkVectorSize, 0);
std::vector<int> vecCpp (benchmarkVectorSize, 0);
std::vector<int> vecCppIter (benchmarkVectorSize, 0);
int main (int argc, const char ** argv)
{
// Preparing input benchmark data (the same data sets for all tests).
srand(0xDEADBEEF);
for (unsigned i = 0; i < vec.size(); i++)
vec[i] = rand();
for (unsigned i = 0; i < benchmarkVectorSize; i++) {
const auto x = rand();
vecBoolinq[i] = x;
vecCpp[i] = x;
vecCppIter[i] = x;
}
// Launch benchmark.
benchmark::Initialize (&argc, argv);
benchmark::RunSpecifiedBenchmarks ();
return EXIT_SUCCESS;
}
TEST(SpeedTest1, BoolinqCode)
static void BM_BoolinqCode (benchmark::State& state)
{
double avgValue = from(vec).where( [](int a){return a%2 == 1;})
while (state.KeepRunning () ) {
double avgValue = from(vecBoolinq).where( [](int a){return a%2 == 1;})
.avg<double>();
double disper = from(vec).where( [](int a){return a%2 == 1;})
double disper = from(vecBoolinq).where( [](int a){return a%2 == 1;})
.select([=](int a){return (double)((a-avgValue)*(a-avgValue));})
.avg<double>();
EXPECT_EQ(164004, (int)(avgValue*10));
EXPECT_EQ(89512454, (int)disper);
// It's no test, instead it's avoiding skip of calculation throgh optimization.
state.PauseTiming ();
EXPECT_TRUE(avgValue);
EXPECT_TRUE(disper);
state.ResumeTiming ();
}
}
BENCHMARK(BM_BoolinqCode);
TEST(SpeedTest1, CppCode)
static void BM_CppCode (benchmark::State& state)
{
while (state.KeepRunning () ) {
double sum = 0;
int count = 0;
for (unsigned i = 0; i < vec.size(); i++)
for (unsigned i = 0; i < vecCpp.size(); i++)
{
if (vec[i] % 2 == 1)
if (vecCpp[i] % 2 == 1)
{
sum += vec[i];
sum += vecCpp[i];
count++;
}
}
double avgValue = sum / count;
double disperSum = 0;
for (unsigned i = 0; i < vec.size(); i++)
if (vec[i] % 2 == 1)
disperSum += (vec[i] - avgValue)*(vec[i] - avgValue);
for (unsigned i = 0; i < vecCpp.size(); i++)
if (vecCpp[i] % 2 == 1)
disperSum += (vecCpp[i] - avgValue)*(vecCpp[i] - avgValue);
double disper = disperSum / count;
EXPECT_EQ(164004, (int)(avgValue*10));
EXPECT_EQ(89512454, (int)disper);
// It's no test, instead it's avoiding skip of calculation throgh optimization.
state.PauseTiming ();
EXPECT_TRUE(avgValue);
EXPECT_TRUE(disper);
state.ResumeTiming ();
}
}
BENCHMARK(BM_CppCode);
TEST(SpeedTest1, CppIterCode)
static void BM_CppIterCode (benchmark::State& state)
{
while (state.KeepRunning () ) {
double sum = 0;
int count = 0;
for (auto it = vec.begin(); it != vec.end(); ++it)
for (auto it = vecCppIter.begin(); it != vecCppIter.end(); ++it)
{
if (*it % 2 == 1)
{
......@@ -67,11 +103,16 @@ TEST(SpeedTest1, CppIterCode)
double avgValue = sum / count;
double disperSum = 0;
for (auto it = vec.begin(); it != vec.end(); ++it)
for (auto it = vecCppIter.begin(); it != vecCppIter.end(); ++it)
if (*it % 2 == 1)
disperSum += (*it - avgValue)*(*it - avgValue);
double disper = disperSum / count;
EXPECT_EQ(164004, (int)(avgValue*10));
EXPECT_EQ(89512454, (int)disper);
// It's no test, instead it's avoiding skip of calculation throgh optimization.
state.PauseTiming ();
EXPECT_TRUE(avgValue);
EXPECT_TRUE(disper);
state.ResumeTiming ();
}
}
BENCHMARK(BM_CppIterCode);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment