Skip to content

Instantly share code, notes, and snippets.

@c9s
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c9s/83ac88d1893e72b869b3 to your computer and use it in GitHub Desktop.
Save c9s/83ac88d1893e72b869b3 to your computer and use it in GitHub Desktop.
/*
* measurement.h
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#ifndef MEASUREMENT_H
#define MEASUREMENT_H
#include <chrono>
#include <fstream>
#include <iostream>
class Measurement {
protected:
std::chrono::high_resolution_clock::time_point start_;
std::chrono::high_resolution_clock::time_point end_;
std::string desc_;
public:
Measurement(std::string desc) {
desc_ = desc;
start_ = std::chrono::high_resolution_clock::now();
std::cout << "Measuring " << desc << "..." << std::endl;
}
~Measurement() {
End();
}
std::chrono::duration<double> Duration() {
return std::chrono::duration_cast<std::chrono::duration<double> >(end_ - start_);
}
void End() {
end_ = std::chrono::high_resolution_clock::now();
std::cout << "Measurement finished. " << desc_ << ": " << Duration().count() << std::endl;
}
};
class MeasurementCsv : Measurement {
std::string filename_;
public:
MeasurementCsv(std::string filename, std::string desc) : Measurement(desc) {
filename_ = filename;
}
~MeasurementCsv() {
Measurement::End();
WriteCSV();
}
void WriteCSV() {
std::ofstream csv_file;
csv_file.open (filename_, std::ofstream::out | std::ofstream::app);
// csv_file << std::chrono::duration_cast<std::chrono::nanoseconds>(end_ - start_).count() << "," << comment_ << std::endl;
csv_file << std::chrono::duration_cast<std::chrono::nanoseconds>(end_ - start_).count() << "ns" << std::endl;
csv_file.close();
}
};
#endif /* !MEASUREMENT_H */
Measuring Matx A * B...
Measurement finished. Matx A * B: 5.121e-05
Measuring Mat A * B ref stack memory...
Measurement finished. Mat A * B ref stack memory: 1.04867
Measuring Mat A * B ref heap memory...
Measurement finished. Mat A * B ref heap memory: 1.52618
/*
* opencv mat benchmark
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "measurement.h"
using namespace cv;
int main(int argc, char** argv) {
/*
int m[2][2] = {{2,3},{1,0}};
Mat M = Mat(2,2,CV_64F,m);
M.at<int>(0,0) = 10;
*/
// printf("%d\n", m[0][0]);
Matx<float, 2,2> Ax( 1,1,1,1 );
Matx<float, 2,2> Bx( 2,2,2,2 );
Matx<float, 2,2> Cx;
int a[2][2] = {{1,1}, {1,1}};
int b[2][2] = {{2,2}, {2,2}};
Mat A = Mat(2,2,CV_64F, a);
Mat B = Mat(2,2,CV_64F, b);
Mat C;
unsigned int CNT = 5000000;
{
Measurement m("Matx A * B");
for (int i = 0; i < CNT ; i++) {
Cx = Ax * Bx;
}
}
{
Measurement m("Mat A * B ref stack memory");
for (int i = 0; i < CNT ; i++) {
C = A * B;
}
}
A = Mat(2,2,CV_64F, Scalar(1));
B = Mat(2,2,CV_64F, Scalar(2));
{
Measurement m("Mat A * B ref heap memory");
for (int i = 0; i < CNT ; i++) {
C = A * B;
}
}
return 0;
}
@c9s
Copy link
Author

c9s commented Jul 30, 2014

gcc -O3 -Isrc -stdlib=libc++ -Wall $(pkg-config --cflags opencv) -lc++ -o test $(pkg-config --libs opencv) test.cc && ./test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment