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