Minor performance tweak to clock FromString() methods
Both the monotonic_clock:: and realtime_clock::FromString()
methods use std::string_view::substr() heavily to process
different parts of the incoming string. In a few cases, it
is extracting a single character out of the string, which is
suboptimal performance-wise. The substr() function constructs
a new string_view object on the stack, and then invokes its
operator==() against a 1-character string literal. It is more
efficient to use operator[]() to extract the single character
and use the built in C comparison of char values.
Rewriting these methods to use regex's might be a good idea,
but I don't know how much that would hit the heap memory
manager, which is unadvisable.
Change-Id: Idb42bb4dd9d2b6502e0cabbd0dd1fbdbbadb3489
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/time/time.cc b/aos/time/time.cc
index a956f94..05510e9 100644
--- a/aos/time/time.cc
+++ b/aos/time/time.cc
@@ -91,11 +91,11 @@
return std::nullopt;
}
- if (now.substr(now.size() - 13, 1) != ".") {
+ if (now[now.size() - 13] != '.') {
return std::nullopt;
}
- bool negative = now.substr(0, 1) == "-";
+ bool negative = now[0] == '-';
std::string sec(
now.substr(negative ? 1 : 0, now.size() - (negative ? 14 : 13)));
@@ -119,7 +119,7 @@
return std::nullopt;
}
- if (now.substr(now.size() - 10, 1) != ".") {
+ if (now[now.size() - 10] != '.') {
return std::nullopt;
}