Skip to content

Commit 907e1cc

Browse files
committed
perf(clock): use native bash arithmetic instead of bc in clock::now
Replace bashunit::math::calculate (which pipes to bc) with native bash $(()) arithmetic in the shell and date-seconds clock paths. Eliminates 1-2 subprocess forks per clock::now call.
1 parent 1132ba9 commit 907e1cc

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/clock.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,19 @@ EOF
108108
date-seconds)
109109
local seconds
110110
seconds=$(date +%s)
111-
bashunit::math::calculate "$seconds * 1000000000"
111+
echo "$((seconds * 1000000000))"
112112
;;
113113
shell)
114114
# shellcheck disable=SC2155
115115
local shell_time="$(bashunit::clock::shell_time)"
116116
local seconds="${shell_time%%.*}"
117117
local microseconds="${shell_time#*.}"
118-
bashunit::math::calculate "($seconds * 1000000000) + ($microseconds * 1000)"
118+
# Pad to 6 digits and strip leading zeros for arithmetic
119+
microseconds="${microseconds}000000"
120+
microseconds="${microseconds:0:6}"
121+
microseconds="${microseconds#"${microseconds%%[!0]*}"}"
122+
microseconds="${microseconds:-0}"
123+
echo "$(( (seconds * 1000000000) + (microseconds * 1000) ))"
119124
;;
120125
*)
121126
bashunit::clock::_choose_impl || return 1

0 commit comments

Comments
 (0)