1 /* Copyright 2018 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 #include "tensorflow/lite/profiling/time.h" 16 17 #if defined(_MSC_VER) 18 #include <chrono> // NOLINT(build/c++11) 19 #include <thread> // NOLINT(build/c++11) 20 #else 21 #include <sys/time.h> 22 #include <time.h> 23 #endif 24 25 namespace tflite { 26 namespace profiling { 27 namespace time { 28 29 #if defined(_MSC_VER) 30 NowMicros()31uint64_t NowMicros() { 32 return static_cast<uint64_t>( 33 std::chrono::duration_cast<std::chrono::microseconds>( 34 std::chrono::system_clock::now().time_since_epoch()) 35 .count()); 36 } 37 SleepForMicros(uint64_t micros)38void SleepForMicros(uint64_t micros) { 39 std::this_thread::sleep_for(std::chrono::microseconds(micros)); 40 } 41 42 #else 43 44 uint64_t NowMicros() { 45 struct timeval tv; 46 gettimeofday(&tv, nullptr); 47 return static_cast<uint64_t>(tv.tv_sec) * 1e6 + tv.tv_usec; 48 } 49 50 void SleepForMicros(uint64_t micros) { 51 timespec sleep_time; 52 sleep_time.tv_sec = micros / 1e6; 53 micros -= sleep_time.tv_sec * 1e6; 54 sleep_time.tv_nsec = micros * 1e3; 55 nanosleep(&sleep_time, nullptr); 56 } 57 58 #endif // defined(_MSC_VER) 59 60 } // namespace time 61 } // namespace profiling 62 } // namespace tflite 63