From 1b7b3c977b8de983dda1ae33d54fe4119b24f9fb Mon Sep 17 00:00:00 2001 From: Syping Date: Sun, 10 Dec 2023 07:57:15 +0100 Subject: [PATCH] libragephoto: add benchmark to C implementation CMakeLists.txt: update RAGEPHOTO_BENCHMARK description RagePhoto.c: add benchmark RagePhoto.cpp: change benchmark clock from std::chrono::high_resolution_clock to std::chrono::steady_clock --- CMakeLists.txt | 2 +- src/core/RagePhoto.c | 17 +++++++++++++++++ src/core/RagePhoto.cpp | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30d842d..4ab47c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,7 @@ else() endif() # RagePhoto Benchmark -option(RAGEPHOTO_BENCHMARK "Build with libragephoto benchmark (C++ only)" OFF) +option(RAGEPHOTO_BENCHMARK "Build with libragephoto benchmark" OFF) # RagePhoto API option(RAGEPHOTO_C_API "Build libragephoto with C API support" ON) diff --git a/src/core/RagePhoto.c b/src/core/RagePhoto.c index b3e3562..599c703 100644 --- a/src/core/RagePhoto.c +++ b/src/core/RagePhoto.c @@ -22,6 +22,10 @@ #include #include +#ifdef RAGEPHOTO_BENCHMARK +#include +#endif + #if defined(UNICODE_ICONV) #include #elif defined(UNICODE_WINCVT) @@ -217,6 +221,11 @@ RagePhotoData* ragephoto_getphotodata(ragephoto_t instance_t) bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser, const char *data, size_t length) { +#ifdef RAGEPHOTO_BENCHMARK + struct timespec benchmark_parse_start; + clock_gettime(CLOCK_MONOTONIC, &benchmark_parse_start); +#endif + // Avoid data conflicts ragephotodata_clear(rp_data); @@ -522,6 +531,14 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser return false; } +#ifdef RAGEPHOTO_BENCHMARK + struct timespec benchmark_parse_end; + clock_gettime(CLOCK_MONOTONIC, &benchmark_parse_end); + const uint64_t benchmark_ns = (UINT64_C(1000000000) * benchmark_parse_end.tv_sec + benchmark_parse_end.tv_nsec) - + (UINT64_C(1000000000) * benchmark_parse_start.tv_sec + benchmark_parse_start.tv_nsec); + printf("Benchmark: %" PRIu64 "ns\n", benchmark_ns); +#endif + #ifdef RAGEPHOTO_DEBUG printf("header: %s\n", rp_data->header); printf("headerSum: %" PRIu32 "\n", rp_data->headerSum); diff --git a/src/core/RagePhoto.cpp b/src/core/RagePhoto.cpp index e3da465..f6c5577 100644 --- a/src/core/RagePhoto.cpp +++ b/src/core/RagePhoto.cpp @@ -219,7 +219,7 @@ RagePhotoData* RagePhoto::data() bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser) { #ifdef RAGEPHOTO_BENCHMARK - auto benchmark_parse_start = std::chrono::high_resolution_clock::now(); + auto benchmark_parse_start = std::chrono::steady_clock::now(); #endif // Avoid data conflicts @@ -541,7 +541,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra } #ifdef RAGEPHOTO_BENCHMARK - auto benchmark_parse_end = std::chrono::high_resolution_clock::now(); + auto benchmark_parse_end = std::chrono::steady_clock::now(); auto benchmark_ns = std::chrono::duration_cast(benchmark_parse_end - benchmark_parse_start); std::cout << "Benchmark: " << benchmark_ns.count() << "ns" << std::endl; #endif