基于our discussion in chat,您未在启用优化的情况下进行编译.如果这样做,您将看到明显的性能提升.另外,请确保您要链接到OpenCV的发行版本.
在未启用优化的情况下,我测量了以下示例的执行时间:
main.cpp中
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
const int num_rows = 32678;
const int num_cols = 10;
const int index_size = 24700;
const int num_runs = 1000;
const int seed = 42;
std::vector index_vec(num_rows);
// fill index with sequence
std::iota (index_vec.begin(), index_vec.end(), 0);
// randomize sequence
std::random_device rd;
std::mt19937 g(rd());
g.seed(seed);
std::shuffle(index_vec.begin(), index_vec.end(), g);
// trunkate index
index_vec.resize(index_size);
cv::Mat w2c(num_rows, num_cols, CV_32F);
// copy
cv::Mat out(index_size, w2c.cols, w2c.type());
auto start = std::chrono::high_resolution_clock::now();
for (int k = 0; k
{
for (int i = 0; i < index_size; ++i)
{
w2c.row(index_vec[i]).copyTo(out.row(i));
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<:chrono::microseconds>(end - start);
std::cout << duration.count()/num_runs << " microseconds" << std::endl;
return 0;
}
的CMakeLists.txt
project(copy)
find_package(OpenCV REQUIRED)
add_executable(copy main.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(copy ${OpenCV_LIBS})
无需优化即可编译运行
cmake . -DCMAKE_BUILD_TYPE=DEBUG
make
./copy
3924 microseconds
编译并优化运行
cmake . -DCMAKE_BUILD_TYPE=RELEASE
make
./copy
2664 microseconds
我对这些进行了测试
>英特尔酷睿i7-4600U CPU
> Ubuntu 14.04(x64)
> GCC 4.8.2
> OpenCV 3.0.0(发行版)