/
githubmr
/
facebook-react-native
Обзор
Документация
Войти
/
githubmr
/
facebook-react-native
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
packages/react-native/ReactCommon/react/nativemodule/cputime/CPUTime.h
83 строки
2 KB
Nick Lefever
Add high resolution CPU time support on macOS (#50553)
09 апр 2025, 14:53
09 апр 2025, 14:53
dc898cc
Код
Авторство
О чём код?
/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #if defined USE_POSIX_TIME #include <time.h> #elif defined __MACH__ #include <mach/mach_time.h> #else #include <chrono> #endif #ifdef USE_POSIX_TIME namespace { const double NANOSECONDS_IN_A_SECOND = 1000000000; } // namespace #endif #ifdef __MACH__ namespace { inline double getConversionFactor() { double conversionFactor; mach_timebase_info_data_t info; mach_timebase_info(&info); conversionFactor = static_cast<double>(info.numer) / info.denom; return conversionFactor; } } // namespace #endif namespace facebook::react { #if defined USE_POSIX_TIME inline double getCPUTimeNanos() { struct timespec time {}; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time); return static_cast<double>(time.tv_sec) * NANOSECONDS_IN_A_SECOND + static_cast<double>(time.tv_nsec); } inline bool hasAccurateCPUTimeNanosForBenchmarks() { return true; } #elif defined __MACH__ inline double getCPUTimeNanos() { static auto conversionFactor = getConversionFactor(); uint64_t time = mach_absolute_time(); return static_cast<double>(time) * conversionFactor; } inline bool hasAccurateCPUTimeNanosForBenchmarks() { return true; } #else inline double getCPUTimeNanos() { auto now = std::chrono::steady_clock::now(); return static_cast<double>( std::chrono::duration_cast<std::chrono::nanoseconds>( now.time_since_epoch()) .count()); } inline bool hasAccurateCPUTimeNanosForBenchmarks() { return false; } #endif } // namespace facebook::react