@@ -18,29 +18,28 @@ limitations under the License.
1818#include " tensorflow/lite/micro/testing/micro_test_v2.h"
1919
2020TEST (MicroTimeTest, TestBasicTimerFunctionality) {
21- uint32_t ticks_per_second = tflite::ticks_per_second ();
22-
23- // Retry enough times to guarantee a tick advance, while not taking too long
24- // to complete. With 1e6 retries, assuming each loop takes tens of cycles,
25- // this will retry for less than 10 seconds on a 10MHz platform.
26- constexpr int kMaxRetries = 1e6 ;
27- unsigned int start_time = tflite::GetCurrentTimeTicks ();
28-
29- if (ticks_per_second != 0 ) {
30- for (int i = 0 ; i < kMaxRetries ; i++) {
31- if (tflite::GetCurrentTimeTicks () - start_time > 0 ) {
32- break ;
33- }
21+ const uint32_t ticks_per_second = tflite::ticks_per_second ();
22+
23+ // If the platform does not implement a timer, skip the test.
24+ if (ticks_per_second == 0 ) {
25+ return ;
26+ }
27+
28+ const auto start_time = tflite::GetCurrentTimeTicks ();
29+
30+ // HARDENING: Increase retries to handle fast CPUs on platforms with low
31+ // timer resolution (e.g., Windows ~15ms). 100 million iterations guarantees
32+ // the loop exceeds the duration of a single tick.
33+ constexpr int kMaxRetries = 100000000 ;
34+
35+ for (volatile int i = 0 ; i < kMaxRetries ; i++) {
36+ if (tflite::GetCurrentTimeTicks () != start_time) {
37+ break ;
3438 }
3539 }
3640
37- // Ensure the timer is increasing. This works for the overflow case too, since
38- // (MIN_INT + x) - (MAX_INT - y) == x + y + 1. For example,
39- // 0x80000001(min int + 1) - 0x7FFFFFFE(max int - 1) = 0x00000003 == 3.
40- // GetTicksPerSecond() == 0 means the timer is not implemented on this
41- // platform.
42- EXPECT_TRUE (ticks_per_second == 0 ||
43- tflite::GetCurrentTimeTicks () - start_time > 0 );
41+ // Verify that time has actually advanced.
42+ EXPECT_GT (tflite::GetCurrentTimeTicks (), start_time);
4443}
4544
4645TF_LITE_MICRO_TESTS_MAIN
0 commit comments