Skip to content

Commit 737a033

Browse files
authored
Merge pull request #13140 from dzhwinter/windows/inference_api
modify the timer
2 parents fbdd4f8 + bfa9b26 commit 737a033

File tree

3 files changed

+46
-43
lines changed

3 files changed

+46
-43
lines changed

paddle/fluid/inference/api/api_impl.cc

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include <sys/time.h>
1615
#include <algorithm>
1716
#include <map>
1817
#include <set>
@@ -23,32 +22,14 @@ limitations under the License. */
2322

2423
#include "paddle/fluid/framework/feed_fetch_method.h"
2524
#include "paddle/fluid/inference/api/api_impl.h"
25+
#include "paddle/fluid/inference/api/timer.h"
2626
#include "paddle/fluid/platform/profiler.h"
2727

2828
DEFINE_bool(profile, false, "Turn on profiler for fluid");
2929

3030
namespace paddle {
3131
namespace {
32-
33-
// Timer for timer
34-
class Timer {
35-
public:
36-
double start;
37-
double startu;
38-
void tic() {
39-
struct timeval tp;
40-
gettimeofday(&tp, NULL);
41-
start = tp.tv_sec;
42-
startu = tp.tv_usec;
43-
}
44-
double toc() {
45-
struct timeval tp;
46-
gettimeofday(&tp, NULL);
47-
double used_time_ms =
48-
(tp.tv_sec - start) * 1000.0 + (tp.tv_usec - startu) / 1000.0;
49-
return used_time_ms;
50-
}
51-
};
32+
using paddle::inference::Timer;
5233

5334
template <class T>
5435
std::string num2str(T a) {
@@ -80,7 +61,7 @@ void NativePaddlePredictor::PrepareFeedFetch() {
8061
bool NativePaddlePredictor::Init(
8162
std::shared_ptr<framework::Scope> parent_scope) {
8263
VLOG(3) << "Predictor::init()";
83-
64+
#if !defined(_WIN32)
8465
if (FLAGS_profile) {
8566
LOG(WARNING) << "Profiler is actived, might affect the performance";
8667
LOG(INFO) << "You can turn off by set gflags '-profile false'";
@@ -89,6 +70,7 @@ bool NativePaddlePredictor::Init(
8970
: platform::ProfilerState::kCPU;
9071
platform::EnableProfiler(tracking_device);
9172
}
73+
#endif
9274

9375
if (config_.use_gpu) {
9476
place_ = paddle::platform::CUDAPlace(config_.device);
@@ -133,10 +115,12 @@ bool NativePaddlePredictor::Init(
133115
}
134116

135117
NativePaddlePredictor::~NativePaddlePredictor() {
118+
#if !defined(_WIN32)
136119
if (FLAGS_profile) {
137120
platform::DisableProfiler(platform::EventSortingKey::kTotal,
138121
"./profile.log");
139122
}
123+
#endif
140124
if (sub_scope_) {
141125
scope_->DeleteScope(sub_scope_);
142126
}

paddle/fluid/inference/api/helper.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,15 @@
1616

1717
#include <sys/time.h>
1818
#include <algorithm>
19-
#include <numeric>
2019
#include <sstream>
2120
#include <string>
2221
#include <vector>
2322
#include "paddle/fluid/inference/api/paddle_inference_api.h"
23+
#include "paddle/fluid/inference/api/timer.h"
2424

2525
namespace paddle {
2626
namespace inference {
2727

28-
// Timer for timer
29-
class Timer {
30-
public:
31-
double start;
32-
double startu;
33-
void tic() {
34-
struct timeval tp;
35-
gettimeofday(&tp, NULL);
36-
start = tp.tv_sec;
37-
startu = tp.tv_usec;
38-
}
39-
double toc() {
40-
struct timeval tp;
41-
gettimeofday(&tp, NULL);
42-
double used_time_ms =
43-
(tp.tv_sec - start) * 1000.0 + (tp.tv_usec - startu) / 1000.0;
44-
return used_time_ms;
45-
}
46-
};
47-
4828
static void split(const std::string &str, char sep,
4929
std::vector<std::string> *pieces) {
5030
pieces->clear();

paddle/fluid/inference/api/timer.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#include <chrono> // NOLINT
17+
18+
namespace paddle {
19+
namespace inference {
20+
21+
// Timer for timer
22+
class Timer {
23+
public:
24+
std::chrono::high_resolution_clock::time_point start;
25+
std::chrono::high_resolution_clock::time_point startu;
26+
27+
void tic() { start = std::chrono::high_resolution_clock::now(); }
28+
double toc() {
29+
startu = std::chrono::high_resolution_clock::now();
30+
std::chrono::duration<double> time_span =
31+
std::chrono::duration_cast<std::chrono::duration<double>>(startu -
32+
start);
33+
double used_time_ms = static_cast<double>(time_span.count()) * 1000.0;
34+
return used_time_ms;
35+
}
36+
};
37+
38+
} // namespace inference
39+
} // namespace paddle

0 commit comments

Comments
 (0)