Created
October 21, 2012 10:10
-
-
Save kazuki-ma/3926595 to your computer and use it in GitHub Desktop.
opencv_speec_test.cpp
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 <cstddef> | |
#include <cstdint> | |
#include <time.h> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#pragma comment(lib, "opencv_core249.lib") | |
#pragma comment(lib, "opencv_highgui249.lib") | |
template<typename _T> | |
void gray_at(cv::Mat_<_T> &M, const size_t iteration = 100) { | |
for (size_t i = 0; i < iteration; ++i) { | |
for (register int r = 0; r < UCHAR_MAX; ++r ) { | |
for(register int c = 0; c < UCHAR_MAX; ++c ) { | |
M.cv::Mat::at<uchar>(r,c) = r; | |
} | |
} | |
} | |
} | |
template<typename _T> | |
void gray_pointer(cv::Mat_<_T> &M, const size_t iteration = 100) { | |
uchar* M_data = M.data; | |
for (size_t i = 0; i < iteration; ++i) { | |
for (int r = 0; r < UCHAR_MAX; ++r) { | |
for (int c = 0; c < UCHAR_MAX; ++c) { | |
M_data[r * UCHAR_MAX + c] = r; | |
} | |
} | |
} | |
} | |
template<typename _T> | |
void gray_fast(cv::Mat_<_T> &M, const size_t iteration = 100){ | |
for (size_t i = 0; i < iteration; ++i) { | |
uchar* M_ptr = M.data; | |
for (int r = 0; r < UCHAR_MAX; ++r) { | |
for (int c = 0; c < UCHAR_MAX; ++c) { | |
M_ptr[c] = r; | |
} | |
M_ptr += M.step[0]; | |
} | |
} | |
} | |
template<typename _T> | |
void color_at (cv::Mat_<_T> &M, const size_t iteration = 100) { | |
for (size_t i = 0; i < iteration; ++i) { | |
for (int r = 0; r < UCHAR_MAX; ++r) { | |
for (int c = 0; c < UCHAR_MAX; ++c) { | |
cv::Vec3b &v = M.at<cv::Vec3b>(r,c); | |
v[0] = r; | |
v[1] = c; | |
v[2] = 0; | |
} | |
} | |
} | |
} | |
template<typename _T> | |
void color_pointer(cv::Mat_<_T> &M, const size_t iteration = 100) { | |
uchar* const M_data = M.data; | |
for (size_t i = 0; i < iteration; ++i) { | |
for (int r = 0; r < UCHAR_MAX; ++r) { | |
for (int c = 0; c < UCHAR_MAX; ++c) { | |
unsigned int offset = 3*(r * UCHAR_MAX + c); | |
M_data[offset] = r; | |
M_data[offset + 1] = c; | |
M_data[offset + 2] = 0; | |
} | |
} | |
} | |
} | |
template<typename _T> | |
void color_fast(cv::Mat_<_T> &M, const size_t iteration = 100){ | |
for (size_t i = 0; i < iteration; ++i) { | |
for (int r = 0; r < UCHAR_MAX; ++r) { | |
uchar* M_ptr = M.data + M.step[0] * r; | |
for (int c = 0; c < UCHAR_MAX; ++c) { | |
*M_ptr++ = r; | |
*M_ptr++ = c; | |
*M_ptr++ = 0; | |
} | |
} | |
} | |
} | |
template<typename _T> | |
void color_iterator (cv::Mat_<_T> &M, const size_t iteration = 100) { | |
for (size_t i = 0; i < iteration; ++i) { | |
for (cv::Mat_<_T>::iterator it = M.begin(); it != M.end(); ++it) { | |
auto position = it.pos(); | |
(*it)[0] = position.y; | |
(*it)[1] = position.x; | |
(*it)[2] = 0; | |
} | |
} | |
} | |
#define BENCH(FUNC, MAT) \ | |
{\ | |
MAT = 0;\ | |
clock_t time = clock();\ | |
FUNC(MAT, bench_size);\ | |
printf("%-20s %10.3fμs/image = %10.1fkHz\n", #FUNC, \ | |
double(clock() - time) / CLOCKS_PER_SEC / bench_size * 1000000, (double)bench_size * CLOCKS_PER_SEC / double(clock() - time) / 1000);\ | |
cv::imshow(#FUNC, MAT);\ | |
} | |
int main(int argc, char **argv) { | |
typedef uint8_t GRAY_T; | |
typedef cv::Vec3b COLOR_T; | |
// Color char matrix | |
const int bench_size = 10000; | |
cv::Mat_<GRAY_T> mc1 = cv::Mat_<GRAY_T>::zeros(UCHAR_MAX, UCHAR_MAX); | |
cv::Mat_<COLOR_T> mc3 = cv::Mat_<COLOR_T>::zeros(UCHAR_MAX, UCHAR_MAX); | |
BENCH(gray_at, mc1); | |
BENCH(gray_pointer, mc1); | |
BENCH(gray_fast, mc1); | |
BENCH(color_at, mc3); | |
BENCH(color_pointer, mc3); | |
BENCH(color_fast, mc3); | |
BENCH(color_iterator, mc3); | |
cv::waitKey(0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My Result
My Machine