33#define YSPARSE_COMMON_HPP_
44
55#include < stdexcept>
6+ #include < c4/yml/export.hpp>
67
78namespace ryml {
89using namespace c4 ;
@@ -18,6 +19,19 @@ struct YsParseError : public std::exception
1819 const char * what () const noexcept override { return msg.c_str (); }
1920};
2021
22+
23+ // -----------------------------------------------------------------------------
24+ // timing
25+
26+ #ifdef __cplusplus
27+ extern " C" {
28+ #endif
29+ RYML_EXPORT bool ysparse_timing_get ();
30+ RYML_EXPORT void ysparse_timing_set (bool yes);
31+ #ifdef __cplusplus
32+ }
33+ #endif
34+
2135#ifndef YSPARSE_TIMED
2236#define TIMED_SECTION (...)
2337#error
@@ -28,22 +42,28 @@ struct YsParseError : public std::exception
2842struct timed_section
2943{
3044 using myclock = std::chrono::steady_clock;
31- ryml::csubstr name;
45+ const char * name;
3246 size_type len;
3347 myclock::time_point start;
34- timed_section (ryml::csubstr n, size_type len_=0 )
35- : name (n)
36- , len (len_)
37- , start (myclock::now ())
48+ C4_NO_INLINE timed_section (const char * n, size_type len_=0 )
3849 {
50+ if (ysparse_timing_get ())
51+ {
52+ name = n;
53+ len = len_;
54+ start = myclock::now ();
55+ }
3956 }
40- ~timed_section ()
57+ C4_NO_INLINE ~timed_section ()
4158 {
42- const std::chrono::duration<float , std::milli> t = myclock::now () - start;
43- fprintf (stderr, " %.6fms: %.*s" , t.count (), (int )name.len , name.str );
44- if (len)
45- fprintf (stderr, " %.3fMB/s" , (float )len / t.count () * 1 .e -3 );
46- fprintf (stderr, " \n " );
59+ if (ysparse_timing_get ())
60+ {
61+ const std::chrono::duration<float , std::milli> t = myclock::now () - start;
62+ fprintf (stderr, " %.6fms: %s" , t.count (), name);
63+ if (len)
64+ fprintf (stderr, " %.3fMB/s" , (float )len / t.count () * 1 .e -3 );
65+ fprintf (stderr, " \n " );
66+ }
4767 }
4868};
4969#endif // YSPARSE_TIMED
0 commit comments