From b583d5d170ee47b80f48d8a617f40202059d4e58 Mon Sep 17 00:00:00 2001 From: jofemodo Date: Thu, 9 Dec 2021 00:41:10 +0100 Subject: [PATCH] Slighty improve the efficiency/precission of delayMicroseconds function by avoiding uneeded CPU cycles. --- wiringPi/wiringPi.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index b16388c..d50b39b 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -2156,16 +2156,15 @@ void delayMicrosecondsHard (unsigned int howLong) void delayMicroseconds (unsigned int howLong) { - struct timespec sleeper ; - unsigned int uSecs = howLong % 1000000 ; - unsigned int wSecs = howLong / 1000000 ; - - /**/ if (howLong == 0) + if (howLong == 0) return ; else if (howLong < 100) delayMicrosecondsHard (howLong) ; else { + struct timespec sleeper ; + unsigned int uSecs = howLong % 1000000 ; + unsigned int wSecs = howLong / 1000000 ; sleeper.tv_sec = wSecs ; sleeper.tv_nsec = (long)(uSecs * 1000L) ; nanosleep (&sleeper, NULL) ;