Commit 14e241fa authored by John A's avatar John A

Time growth faster, improve comments

Growth is now timed using 'time -p' instead of 'date', which drastically
reduces the resources required to keep track of each growth step. This
speeds up growth and reduces CPU usage.
parent 3de20680
......@@ -345,7 +345,7 @@ init() {
done
unset IFS # reset delimiter
# -- logic --
# declare vars
branches=0
shoots=0
......@@ -364,6 +364,9 @@ init() {
stty -echo # don't echo stdin
printf '%b' '\e[?25l\e[?7l\e[2J' # hide cursor, disable line wrapping, clear screen and move to 0,0
fi
# setup temp file for caching times of each growth
tmpTime="$(tempfile -p tmpTime)"
}
grow() {
......@@ -578,15 +581,11 @@ branch() {
# print what we have so far
if ((live)); then
begin="$(date +%s.%N)" # start timer
display
( time -p display ) 2>"$tmpTime"
elapsed="$(head "$tmpTime" -n 1 | awk '{print $2}' )"
# if this step took less than $stepTime, sleep until $stepTime is met
if ((live)); then
end="$(date +%s.%N)" # end timer
timeLeft="$(printf '%s\n' "($timeStep - ($end - $begin))" | bc -l)"
[ "$(printf '%s\n' "$timeLeft > 0" | bc -l)" -eq 1 ] && sleep "$timeLeft"
fi
timeLeft="$(printf '%s\n' "$timeStep - $elapsed" | bc -l)"
[ "$(printf '%s\n' "($timeLeft) > 0" | bc -l)" -eq 1 ] && sleep "$timeLeft"
fi
done
}
......@@ -601,7 +600,7 @@ display() {
for item in "${changed[@]}"; do
if [ "$row" = "$item" ]; then
for (( col=0; col <= cols; col++ )); do
((live)) && printf '%b' '\e[0;0H' # moves cursor to 0,0
((live)) && printf '%b' '\e[0;0H' # move cursor to 0,0
# grab the character from our grid
char="${grid[$row,$col]}"
......@@ -629,11 +628,12 @@ display() {
quit() {
if ((! nfetch)); then
stty echo # echo stdin
stty echo # echo stdin
printf '%b\n' '\e[?25h\e[?7h'"${R}" # show cursor, enable line wrapping, reset colors
else
printf '\n' # reset formatting, put cursor on next line
printf '\n' # reset formatting, put cursor on next line
fi
rm "$tmpTime"
exit 0
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment