perf_math.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "perf_precomp.hpp"
  2. namespace opencv_test
  3. {
  4. using namespace perf;
  5. namespace {
  6. typedef perf::TestBaseWithParam<size_t> VectorLength;
  7. PERF_TEST_P(VectorLength, phase32f, testing::Values(128, 1000, 128*1024, 512*1024, 1024*1024))
  8. {
  9. size_t length = GetParam();
  10. vector<float> X(length);
  11. vector<float> Y(length);
  12. vector<float> angle(length);
  13. declare.in(X, Y, WARMUP_RNG).out(angle);
  14. TEST_CYCLE_N(200) cv::phase(X, Y, angle, true);
  15. SANITY_CHECK(angle, 5e-5);
  16. }
  17. PERF_TEST_P(VectorLength, phase64f, testing::Values(128, 1000, 128*1024, 512*1024, 1024*1024))
  18. {
  19. size_t length = GetParam();
  20. vector<double> X(length);
  21. vector<double> Y(length);
  22. vector<double> angle(length);
  23. declare.in(X, Y, WARMUP_RNG).out(angle);
  24. TEST_CYCLE_N(200) cv::phase(X, Y, angle, true);
  25. SANITY_CHECK(angle, 5e-5);
  26. }
  27. typedef perf::TestBaseWithParam< testing::tuple<int, int, int> > KMeans;
  28. PERF_TEST_P_(KMeans, single_iter)
  29. {
  30. RNG& rng = theRNG();
  31. const int K = testing::get<0>(GetParam());
  32. const int dims = testing::get<1>(GetParam());
  33. const int N = testing::get<2>(GetParam());
  34. const int attempts = 5;
  35. Mat data(N, dims, CV_32F);
  36. rng.fill(data, RNG::UNIFORM, -0.1, 0.1);
  37. const int N0 = K;
  38. Mat data0(N0, dims, CV_32F);
  39. rng.fill(data0, RNG::UNIFORM, -1, 1);
  40. for (int i = 0; i < N; i++)
  41. {
  42. int base = rng.uniform(0, N0);
  43. cv::add(data0.row(base), data.row(i), data.row(i));
  44. }
  45. declare.in(data);
  46. Mat labels, centers;
  47. TEST_CYCLE()
  48. {
  49. kmeans(data, K, labels, TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 1, 0),
  50. attempts, KMEANS_PP_CENTERS, centers);
  51. }
  52. SANITY_CHECK_NOTHING();
  53. }
  54. PERF_TEST_P_(KMeans, good)
  55. {
  56. RNG& rng = theRNG();
  57. const int K = testing::get<0>(GetParam());
  58. const int dims = testing::get<1>(GetParam());
  59. const int N = testing::get<2>(GetParam());
  60. const int attempts = 5;
  61. Mat data(N, dims, CV_32F);
  62. rng.fill(data, RNG::UNIFORM, -0.1, 0.1);
  63. const int N0 = K;
  64. Mat data0(N0, dims, CV_32F);
  65. rng.fill(data0, RNG::UNIFORM, -1, 1);
  66. for (int i = 0; i < N; i++)
  67. {
  68. int base = rng.uniform(0, N0);
  69. cv::add(data0.row(base), data.row(i), data.row(i));
  70. }
  71. declare.in(data);
  72. Mat labels, centers;
  73. TEST_CYCLE()
  74. {
  75. kmeans(data, K, labels, TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 30, 0),
  76. attempts, KMEANS_PP_CENTERS, centers);
  77. }
  78. SANITY_CHECK_NOTHING();
  79. }
  80. PERF_TEST_P_(KMeans, with_duplicates)
  81. {
  82. RNG& rng = theRNG();
  83. const int K = testing::get<0>(GetParam());
  84. const int dims = testing::get<1>(GetParam());
  85. const int N = testing::get<2>(GetParam());
  86. const int attempts = 5;
  87. Mat data(N, dims, CV_32F, Scalar::all(0));
  88. const int N0 = std::max(2, K * 2 / 3);
  89. Mat data0(N0, dims, CV_32F);
  90. rng.fill(data0, RNG::UNIFORM, -1, 1);
  91. for (int i = 0; i < N; i++)
  92. {
  93. int base = rng.uniform(0, N0);
  94. data0.row(base).copyTo(data.row(i));
  95. }
  96. declare.in(data);
  97. Mat labels, centers;
  98. TEST_CYCLE()
  99. {
  100. kmeans(data, K, labels, TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 30, 0),
  101. attempts, KMEANS_PP_CENTERS, centers);
  102. }
  103. SANITY_CHECK_NOTHING();
  104. }
  105. INSTANTIATE_TEST_CASE_P(/*nothing*/ , KMeans,
  106. testing::Values(
  107. // K clusters, dims, N points
  108. testing::make_tuple(2, 3, 100000),
  109. testing::make_tuple(4, 3, 500),
  110. testing::make_tuple(4, 3, 1000),
  111. testing::make_tuple(4, 3, 10000),
  112. testing::make_tuple(8, 3, 1000),
  113. testing::make_tuple(8, 16, 1000),
  114. testing::make_tuple(8, 64, 1000),
  115. testing::make_tuple(16, 16, 1000),
  116. testing::make_tuple(16, 32, 1000),
  117. testing::make_tuple(32, 16, 1000),
  118. testing::make_tuple(32, 32, 1000),
  119. testing::make_tuple(100, 2, 1000)
  120. )
  121. );
  122. }
  123. } // namespace