Last active
April 14, 2019 15:17
-
-
Save matt-42/756abba4ba5aaf1f256e37e8e142941b to your computer and use it in GitHub Desktop.
cvmat_bench
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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