Skip to content

Instantly share code, notes, and snippets.

@matt-42 matt-42/cvmat_bench.cc
Last active Apr 14, 2019

Embed
What would you like to do?
cvmat_bench
#include <opencv2/opencv.hpp>
#include <chrono>
class timer
{
public:
void start() { start_ = std::chrono::high_resolution_clock::now(); }
void end() { end_ = std::chrono::high_resolution_clock::now(); }
unsigned long us() const {
return std::chrono::duration_cast<std::chrono::microseconds>(end_ - start_).count();
}
unsigned long ms() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(end_ - start_).count();
}
unsigned long ns() const {
return std::chrono::duration_cast<std::chrono::nanoseconds>(end_ - start_).count();
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> start_, end_;
};
typedef unsigned char S;
template <typename S>
void test()
{
timer t;
cv::Mat_<S> A(1000, 1000);
cv::Mat_<S> B(1000, 1000);
cv::Mat_<S> C(1000, 1000);
const auto domain = A.size;
const int nrows = 1000;
const int ncols = 1000;
B.setTo(S(42));
C.setTo(S(42));
t.start();
for (int K = 0; K < 100; K++)
for (int r = 0; r < nrows; r++)
for (int c = 0; c < ncols; c++)
A(r, c) = B(r, c) + C(r, c);
t.end();
std::cout << t.us() << " " << int(A(10,10)) << std::endl;
}
int main(int argc, char* argv[])
{
test<unsigned char>();
test<int>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.