Created
April 24, 2020 03:15
-
-
Save syohex/55e8876462f3b63e1cbb8a0270c7a6eb to your computer and use it in GitHub Desktop.
sample code of mach_absolute_time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
#include <cstdio> | |
#include <cinttypes> | |
#include <mach/mach_time.h> | |
namespace { | |
void mach_ver() { | |
mach_timebase_info_data_t timebase_info; | |
mach_timebase_info(&timebase_info); | |
int64_t base = mach_absolute_time() * timebase_info.numer / timebase_info.denom; | |
int64_t diffs[10]; | |
for (int i = 0; i < 10; ++i) { | |
int64_t now = mach_absolute_time() * timebase_info.numer / timebase_info.denom; | |
diffs[i] = now - base; | |
base = now; | |
} | |
for (int i = 0; i < 10; ++i) { | |
printf("mach[%2d] = %" PRId64 "\n", i, diffs[i]); | |
} | |
} | |
void chrono_ver() { | |
auto base = std::chrono::system_clock::now(); | |
int64_t diffs[10]; | |
for (int i = 0; i < 10; ++i) { | |
auto now = std::chrono::system_clock::now(); | |
diffs[i] = std::chrono::duration_cast<std::chrono::nanoseconds>(now - base).count(); | |
base = now; | |
} | |
for (int i = 0; i < 10; ++i) { | |
printf("chrono[%2d] = %" PRId64 "\n", i, diffs[i]); | |
} | |
} | |
} // namespace | |
int main() { | |
mach_ver(); | |
chrono_ver(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment