#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_CATCH2)
#include <numeric>
#include <catch_amalgamated.hpp>
#include <visp3/core/vpColVector.h>
#ifdef ENABLE_VISP_NAMESPACE
#endif
namespace
{
static bool g_runBenchmark = false;
static const std::vector<unsigned int> g_sizes = { 23, 127, 233, 419, 1153, 2749 };
double getRandomValues(double min, double max) { return (max - min) * ((double)rand() / (double)RAND_MAX) + min; }
vpColVector generateRandomVector(
unsigned int rows,
double min = -100,
double max = 100)
{
for (
unsigned int i = 0; i < v.
getRows(); i++) {
v[i] = getRandomValues(min, max);
}
return v;
}
double stddev(const std::vector<double> &vec)
{
double sum = std::accumulate(vec.begin(), vec.end(), 0.0);
double mean = sum / vec.size();
std::vector<double> diff(vec.size());
std::transform(vec.begin(), vec.end(), diff.begin(), [mean](double x) {
return x - mean; });
double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
return std::sqrt(sq_sum / vec.size());
}
{
double sum = 0.0;
for (
unsigned int i = 0; i < v.
getRows(); i++) {
sum += v[i];
}
return sum;
}
{
double sum_square = 0.0;
for (
unsigned int i = 0; i < v.
getRows(); i++) {
sum_square += v[i] * v[i];
}
return sum_square;
}
{
double mean_value = computeRegularSum(v) / v.
getRows();
double sum_squared_diff = 0.0;
for (
unsigned int i = 0; i < v.
size(); i++) {
sum_squared_diff += (v[i] - mean_value) * (v[i] - mean_value);
}
double divisor = (double)v.
size();
return std::sqrt(sum_squared_diff / divisor);
}
std::vector<double> computeHadamard(const std::vector<double> &v1, const std::vector<double> &v2)
{
std::vector<double> result;
std::transform(v1.begin(), v1.end(), v2.begin(), std::back_inserter(result), std::multiplies<double>());
return result;
}
{
if (v1.getRows() != v2.getRows()) {
return false;
}
for (unsigned int i = 0; i < v1.getRows(); i++) {
return false;
}
}
return true;
}
}
TEST_CASE("Benchmark vpColVector::sum()", "[benchmark]")
{
{
const double val = 11;
CHECK(v.sum() == Catch::Approx(val).epsilon(std::numeric_limits<double>::epsilon()));
}
{
const unsigned int size = 11;
std::vector<double> vec(size);
for (size_t i = 0; i < 11; i++) {
vec[i] = 2. * i;
v[static_cast<unsigned int>(i)] = vec[i];
}
CHECK(v.sum() == Catch::Approx(std::accumulate(vec.begin(), vec.end(), 0.0)).epsilon(std::numeric_limits<double>::epsilon()));
}
if (g_runBenchmark) {
for (auto size : g_sizes) {
std::ostringstream oss;
oss << "Benchmark vpColVector::sum() with size: " << size << " (ViSP)";
double vp_sum = 0;
BENCHMARK(oss.str().c_str())
{
return vp_sum;
};
oss.str("");
oss << "Benchmark std::accumulate() with size: " << size << " (C++)";
double std_sum = 0;
BENCHMARK(oss.str().c_str())
{
std_sum = std::accumulate(vec.begin(), vec.end(), 0.0);
return std_sum;
};
CHECK(vp_sum == Catch::Approx(std_sum));
oss.str("");
oss << "Benchmark naive sum() with size: " << size;
double naive_sum = 0;
BENCHMARK(oss.str().c_str())
{
naive_sum = computeRegularSum(v);
return naive_sum;
};
CHECK(naive_sum == Catch::Approx(std_sum));
}
}
}
TEST_CASE("Benchmark vpColVector::sumSquare()", "[benchmark]")
{
{
const double val = 11;
CHECK(v.
sumSquare() == Catch::Approx(val * val).epsilon(std::numeric_limits<double>::epsilon()));
}
{
const unsigned int size = 11;
std::vector<double> vec(size);
for (size_t i = 0; i < 11; i++) {
vec[i] = 2. * i;
v[static_cast<unsigned int>(i)] = vec[i];
}
CHECK(v.
sumSquare() == Catch::Approx(std::inner_product(vec.begin(), vec.end(), vec.begin(), 0.0))
.epsilon(std::numeric_limits<double>::epsilon()));
}
if (g_runBenchmark) {
for (auto size : g_sizes) {
std::ostringstream oss;
oss << "Benchmark vpColVector::sumSquare() with size: " << size << " (ViSP)";
double vp_sq_sum = 0;
BENCHMARK(oss.str().c_str())
{
return vp_sq_sum;
};
oss.str("");
oss << "Benchmark std::inner_product with size: " << size << " (C++)";
double std_sq_sum = 0;
BENCHMARK(oss.str().c_str())
{
std_sq_sum = std::inner_product(vec.begin(), vec.end(), vec.begin(), 0.0);
return std_sq_sum;
};
CHECK(vp_sq_sum == Catch::Approx(std_sq_sum));
oss.str("");
oss << "Benchmark naive sumSquare() with size: " << size;
double naive_sq_sum = 0;
BENCHMARK(oss.str().c_str())
{
naive_sq_sum = computeRegularSumSquare(v);
return naive_sq_sum;
};
CHECK(naive_sq_sum == Catch::Approx(std_sq_sum));
}
}
}
TEST_CASE("Benchmark vpColVector::stdev()", "[benchmark]")
{
{
v[0] = 11;
v[1] = 16;
CHECK(
vpColVector::stdev(v) == Catch::Approx(stddev(vec)).epsilon(std::numeric_limits<double>::epsilon()));
}
{
const unsigned int size = 11;
std::vector<double> vec(size);
for (size_t i = 0; i < 11; i++) {
vec[i] = 2. * i;
v[static_cast<unsigned int>(i)] = vec[i];
}
CHECK(
vpColVector::stdev(v) == Catch::Approx(stddev(vec)).epsilon(std::numeric_limits<double>::epsilon()));
}
if (g_runBenchmark) {
for (auto size : g_sizes) {
std::ostringstream oss;
oss << "Benchmark vpColVector::stdev() with size: " << size << " (ViSP)";
double vp_stddev = 0;
BENCHMARK(oss.str().c_str())
{
return vp_stddev;
};
oss.str("");
oss << "Benchmark C++ stddev impl with size: " << size << " (C++)";
double std_stddev = 0;
BENCHMARK(oss.str().c_str())
{
std_stddev = stddev(vec);
return std_stddev;
};
CHECK(vp_stddev == Catch::Approx(std_stddev));
oss.str("");
oss << "Benchmark naive stdev() with size: " << size;
double naive_stddev = 0;
BENCHMARK(oss.str().c_str())
{
naive_stddev = computeRegularStdev(v);
return naive_stddev;
};
CHECK(naive_stddev == Catch::Approx(std_stddev));
}
}
}
TEST_CASE("Benchmark vpColVector::hadamard()", "[benchmark]")
{
{
v1[0] = 11;
v1[1] = 16;
v2[0] = 8;
v2[1] = 23;
vpColVector res2(computeHadamard(v1.toStdVector(), v2.toStdVector()));
CHECK(almostEqual(res1, res2));
}
{
const unsigned int size = 11;
std::vector<double> vec1(size), vec2(size);
for (size_t i = 0; i < 11; i++) {
vec1[i] = 2. * i;
vec2[i] = 3. * i + 5.;
}
vpColVector res2(computeHadamard(v1.toStdVector(), v2.toStdVector()));
CHECK(almostEqual(res1, res2));
}
if (g_runBenchmark) {
for (auto size : g_sizes) {
std::ostringstream oss;
oss << "Benchmark vpColVector::hadamard() with size: " << size << " (ViSP)";
BENCHMARK(oss.str().c_str())
{
return vp_res;
};
oss.str("");
oss << "Benchmark C++ element wise multiplication with size: " << size << " (C++)";
std::vector<double> std_res;
BENCHMARK(oss.str().c_str())
{
std_res = computeHadamard(vec1, vec2);
return std_res;
};
}
}
}
int main(int argc, char *argv[])
{
srand(1);
Catch::Session session;
auto cli = session.cli()
| Catch::Clara::Opt(g_runBenchmark)["--benchmark"]("run benchmark?");
session.cli(cli);
session.applyCommandLine(argc, argv);
int numFailed = session.run();
return numFailed;
}
#else
#include <iostream>
int main() { return EXIT_SUCCESS; }
#endif
unsigned int size() const
Return the number of elements of the 2D array.
unsigned int getRows() const
Implementation of column vector and the associated operations.
vpColVector hadamard(const vpColVector &v) const
std::vector< double > toStdVector() const
static double stdev(const vpColVector &v, bool useBesselCorrection=false)
static bool equal(double x, double y, double threshold=0.001)