Skip to content

Instantly share code, notes, and snippets.

@kazuki-ma
Created October 21, 2012 10:10
Show Gist options
  • Save kazuki-ma/3926595 to your computer and use it in GitHub Desktop.
Save kazuki-ma/3926595 to your computer and use it in GitHub Desktop.
opencv_speec_test.cpp
#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;
}
@kazuki-ma
Copy link
Author

My Result

gray_at                  79.200μs/image =       12.6kHz
gray_pointer              9.300μs/image =      107.5kHz
gray_fast                 9.400μs/image =      106.4kHz
color_at                123.800μs/image =        8.1kHz
color_pointer            83.400μs/image =       12.0kHz
color_fast               76.800μs/image =       13.0kHz
color_iterator         1638.000μs/image =        0.6kHz

My Machine

------------------
System Information
------------------
Time of this report: 10/21/2012, 19:12:33
   Operating System: Windows 7 Ultimate 64-bit (6.1, Build 7601) Service Pack 1 (7601.win7sp1_gdr.120830-0333)
           Language: Japanese (Regional Setting: Japanese)
System Manufacturer: Gigabyte Technology Co., Ltd.
       System Model: EX58-UD3R
               BIOS: Award Modular BIOS v6.00PG
          Processor: Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz (8 CPUs), ~2.7GHz
             Memory: 6144MB RAM
Available OS Memory: 6142MB RAM
          Page File: 4508MB used, 7774MB available
        Windows Dir: C:\Windows
    DirectX Version: DirectX 11
DX Setup Parameters: Not found
   User DPI Setting: Using System DPI
 System DPI Setting: 96 DPI (100 percent)
    DWM DPI Scaling: Disabled
     DxDiag Version: 6.01.7601.17514 32bit Unicode

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