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 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "benchmark.h"
  15. #if NCNN_BENCHMARK
  16. #include <stdio.h>
  17. #include "layer/convolution.h"
  18. #ifdef _WIN32
  19. #define WIN32_LEAN_AND_MEAN
  20. #include <Windows.h>
  21. #include <stdint.h> // portable: uint64_t MSVC: __int64
  22. #else // _WIN32
  23. #include <sys/time.h>
  24. #endif // _WIN32
  25. namespace ncnn {
  26. #ifdef _WIN32
  27. struct timeval get_current_time()
  28. {
  29. // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
  30. // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
  31. // until 00:00:00 January 1, 1970
  32. static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
  33. SYSTEMTIME system_time;
  34. FILETIME file_time;
  35. uint64_t time;
  36. GetSystemTime( &system_time );
  37. SystemTimeToFileTime( &system_time, &file_time );
  38. time = ((uint64_t)file_time.dwLowDateTime ) ;
  39. time += ((uint64_t)file_time.dwHighDateTime) << 32;
  40. struct timeval tp;
  41. tp.tv_sec = (long) ((time - EPOCH) / 10000000L);
  42. tp.tv_usec = (long) (system_time.wMilliseconds * 1000);
  43. return tp;
  44. }
  45. #else // _WIN32
  46. struct timeval get_current_time()
  47. {
  48. struct ::timeval tv;
  49. ::gettimeofday(&tv, NULL);
  50. struct timeval tp = { tv.tv_sec, tv.tv_usec };
  51. return tp;
  52. }
  53. #endif // _WIN32
  54. double time_elapsed(struct timeval start, struct timeval end)
  55. {
  56. return (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0;
  57. }
  58. void benchmark(const ncnn::Layer* layer, struct timeval start, struct timeval end)
  59. {
  60. fprintf(stderr, "%-24s %-24s %8.2lfms", layer->type.c_str(), layer->name.c_str(), time_elapsed(start, end));
  61. fprintf(stderr, " |");
  62. fprintf(stderr, "\n");
  63. }
  64. void benchmark(const Layer* layer, const Mat& bottom_blob, Mat& top_blob, struct timeval start, struct timeval end)
  65. {
  66. fprintf(stderr, "%-24s %-24s %8.2lfms", layer->type.c_str(), layer->name.c_str(), time_elapsed(start, end));
  67. fprintf(stderr, " | feature_map: %4d x %-4d inch: %4d outch: %4d", bottom_blob.w, bottom_blob.h, bottom_blob.c, top_blob.c);
  68. if (layer->type == "Convolution")
  69. {
  70. fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
  71. ((Convolution*)layer)->kernel_w,
  72. ((Convolution*)layer)->kernel_h,
  73. ((Convolution*)layer)->stride_w,
  74. ((Convolution*)layer)->stride_h
  75. );
  76. }
  77. fprintf(stderr, "\n");
  78. }
  79. } // namespace ncnn
  80. #endif // NCNN_BENCHMARK