1 /* Copyright 2019 The TensorFlow 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 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_UTILS_H_
17 #define TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_UTILS_H_
18 
19 #include <sstream>
20 #include <string>
21 #include <vector>
22 
23 namespace tflite {
24 namespace benchmark {
25 namespace util {
26 
27 // A convenient function that wraps tflite::profiling::time::SleepForMicros and
28 // simply return if 'sleep_seconds' is negative.
29 void SleepForSeconds(double sleep_seconds);
30 
31 // Split the 'str' according to 'delim', and store each splitted element into
32 // 'values'.
33 template <typename T>
SplitAndParse(const std::string & str,char delim,std::vector<T> * values)34 bool SplitAndParse(const std::string& str, char delim, std::vector<T>* values) {
35   std::istringstream input(str);
36   for (std::string line; std::getline(input, line, delim);) {
37     std::istringstream to_parse(line);
38     T val;
39     to_parse >> val;
40     if (!to_parse.eof() && !to_parse.good()) {
41       return false;
42     }
43     values->emplace_back(val);
44   }
45   return true;
46 }
47 
48 }  // namespace util
49 }  // namespace benchmark
50 }  // namespace tflite
51 
52 #endif  // TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_UTILS_H_
53