You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

benchmark.cpp 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifdef _WIN32
  15. #define WIN32_LEAN_AND_MEAN
  16. #include <windows.h>
  17. #else // _WIN32
  18. #include <sys/time.h>
  19. #endif // _WIN32
  20. #include "benchmark.h"
  21. #if NCNN_BENCHMARK
  22. #include "layer/convolution.h"
  23. #include "layer/convolutiondepthwise.h"
  24. #include "layer/deconvolution.h"
  25. #include "layer/deconvolutiondepthwise.h"
  26. #include <stdio.h>
  27. #endif // NCNN_BENCHMARK
  28. namespace ncnn {
  29. double get_current_time()
  30. {
  31. #ifdef _WIN32
  32. LARGE_INTEGER freq;
  33. LARGE_INTEGER pc;
  34. QueryPerformanceFrequency(&freq);
  35. QueryPerformanceCounter(&pc);
  36. return pc.QuadPart * 1000.0 / freq.QuadPart;
  37. #else // _WIN32
  38. struct timeval tv;
  39. gettimeofday(&tv, NULL);
  40. return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
  41. #endif // _WIN32
  42. }
  43. #if NCNN_BENCHMARK
  44. void benchmark(const Layer* layer, double start, double end)
  45. {
  46. fprintf(stderr, "%-24s %-30s %8.2lfms", layer->type.c_str(), layer->name.c_str(), end - start);
  47. fprintf(stderr, " |");
  48. fprintf(stderr, "\n");
  49. }
  50. void benchmark(const Layer* layer, const Mat& bottom_blob, Mat& top_blob, double start, double end)
  51. {
  52. fprintf(stderr, "%-24s %-30s %8.2lfms", layer->type.c_str(), layer->name.c_str(), end - start);
  53. char in_shape_str[64] = {'\0'};
  54. char out_shape_str[64] = {'\0'};
  55. if (bottom_blob.dims == 1)
  56. {
  57. sprintf(in_shape_str, "[%3d *%d]", bottom_blob.w, bottom_blob.elempack);
  58. }
  59. if (bottom_blob.dims == 2)
  60. {
  61. sprintf(in_shape_str, "[%3d, %3d *%d]", bottom_blob.w, bottom_blob.h, bottom_blob.elempack);
  62. }
  63. if (bottom_blob.dims == 3)
  64. {
  65. sprintf(in_shape_str, "[%3d, %3d, %3d *%d]", bottom_blob.w, bottom_blob.h, bottom_blob.c, bottom_blob.elempack);
  66. }
  67. if (top_blob.dims == 1)
  68. {
  69. sprintf(out_shape_str, "[%3d *%d]", top_blob.w, top_blob.elempack);
  70. }
  71. if (top_blob.dims == 2)
  72. {
  73. sprintf(out_shape_str, "[%3d, %3d *%d]", top_blob.w, top_blob.h, top_blob.elempack);
  74. }
  75. if (top_blob.dims == 3)
  76. {
  77. sprintf(out_shape_str, "[%3d, %3d, %3d *%d]", top_blob.w, top_blob.h, top_blob.c, top_blob.elempack);
  78. }
  79. fprintf(stderr, " | %22s -> %-22s", in_shape_str, out_shape_str);
  80. if (layer->type == "Convolution")
  81. {
  82. fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
  83. ((Convolution*)layer)->kernel_w,
  84. ((Convolution*)layer)->kernel_h,
  85. ((Convolution*)layer)->stride_w,
  86. ((Convolution*)layer)->stride_h);
  87. }
  88. else if (layer->type == "ConvolutionDepthWise")
  89. {
  90. fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
  91. ((ConvolutionDepthWise*)layer)->kernel_w,
  92. ((ConvolutionDepthWise*)layer)->kernel_h,
  93. ((ConvolutionDepthWise*)layer)->stride_w,
  94. ((ConvolutionDepthWise*)layer)->stride_h);
  95. }
  96. else if (layer->type == "Deconvolution")
  97. {
  98. fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
  99. ((Deconvolution*)layer)->kernel_w,
  100. ((Deconvolution*)layer)->kernel_h,
  101. ((Deconvolution*)layer)->stride_w,
  102. ((Deconvolution*)layer)->stride_h);
  103. }
  104. else if (layer->type == "DeconvolutionDepthWise")
  105. {
  106. fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
  107. ((DeconvolutionDepthWise*)layer)->kernel_w,
  108. ((DeconvolutionDepthWise*)layer)->kernel_h,
  109. ((DeconvolutionDepthWise*)layer)->stride_w,
  110. ((DeconvolutionDepthWise*)layer)->stride_h);
  111. }
  112. fprintf(stderr, "\n");
  113. }
  114. #endif // NCNN_BENCHMARK
  115. } // namespace ncnn