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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifdef _WIN32
  30. double get_current_time()
  31. {
  32. LARGE_INTEGER freq;
  33. LARGE_INTEGER pc;
  34. QueryPerformanceFrequency(&freq);
  35. QueryPerformanceCounter(&pc);
  36. return pc.QuadPart * 1000.0 / freq.QuadPart;
  37. }
  38. #else // _WIN32
  39. double get_current_time()
  40. {
  41. struct timeval tv;
  42. gettimeofday(&tv, NULL);
  43. return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
  44. }
  45. #endif // _WIN32
  46. #if NCNN_BENCHMARK
  47. void benchmark(const Layer* layer, double start, double end)
  48. {
  49. fprintf(stderr, "%-24s %-30s %8.2lfms", layer->type.c_str(), layer->name.c_str(), end - start);
  50. fprintf(stderr, " |");
  51. fprintf(stderr, "\n");
  52. }
  53. void benchmark(const Layer* layer, const Mat& bottom_blob, Mat& top_blob, double start, double end)
  54. {
  55. fprintf(stderr, "%-24s %-30s %8.2lfms", layer->type.c_str(), layer->name.c_str(), end - start);
  56. char in_shape_str[64] = {'\0'};
  57. char out_shape_str[64] = {'\0'};
  58. if (bottom_blob.dims == 1)
  59. {
  60. sprintf(in_shape_str, "[%3d *%d]", bottom_blob.w, bottom_blob.elempack);
  61. }
  62. if (bottom_blob.dims == 2)
  63. {
  64. sprintf(in_shape_str, "[%3d, %3d *%d]", bottom_blob.w, bottom_blob.h, bottom_blob.elempack);
  65. }
  66. if (bottom_blob.dims == 3)
  67. {
  68. sprintf(in_shape_str, "[%3d, %3d, %3d *%d]", bottom_blob.w, bottom_blob.h, bottom_blob.c, bottom_blob.elempack);
  69. }
  70. if (top_blob.dims == 1)
  71. {
  72. sprintf(out_shape_str, "[%3d *%d]", top_blob.w, top_blob.elempack);
  73. }
  74. if (top_blob.dims == 2)
  75. {
  76. sprintf(out_shape_str, "[%3d, %3d *%d]", top_blob.w, top_blob.h, top_blob.elempack);
  77. }
  78. if (top_blob.dims == 3)
  79. {
  80. sprintf(out_shape_str, "[%3d, %3d, %3d *%d]", top_blob.w, top_blob.h, top_blob.c, top_blob.elempack);
  81. }
  82. fprintf(stderr, " | %22s -> %-22s", in_shape_str, out_shape_str);
  83. if (layer->type == "Convolution")
  84. {
  85. fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
  86. ((Convolution*)layer)->kernel_w,
  87. ((Convolution*)layer)->kernel_h,
  88. ((Convolution*)layer)->stride_w,
  89. ((Convolution*)layer)->stride_h);
  90. }
  91. else if (layer->type == "ConvolutionDepthWise")
  92. {
  93. fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
  94. ((ConvolutionDepthWise*)layer)->kernel_w,
  95. ((ConvolutionDepthWise*)layer)->kernel_h,
  96. ((ConvolutionDepthWise*)layer)->stride_w,
  97. ((ConvolutionDepthWise*)layer)->stride_h);
  98. }
  99. else if (layer->type == "Deconvolution")
  100. {
  101. fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
  102. ((Deconvolution*)layer)->kernel_w,
  103. ((Deconvolution*)layer)->kernel_h,
  104. ((Deconvolution*)layer)->stride_w,
  105. ((Deconvolution*)layer)->stride_h);
  106. }
  107. else if (layer->type == "DeconvolutionDepthWise")
  108. {
  109. fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
  110. ((DeconvolutionDepthWise*)layer)->kernel_w,
  111. ((DeconvolutionDepthWise*)layer)->kernel_h,
  112. ((DeconvolutionDepthWise*)layer)->stride_w,
  113. ((DeconvolutionDepthWise*)layer)->stride_h);
  114. }
  115. fprintf(stderr, "\n");
  116. }
  117. #endif // NCNN_BENCHMARK
  118. } // namespace ncnn