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() { ...@@ -345,7 +345,7 @@ init() {
done done
unset IFS # reset delimiter unset IFS # reset delimiter
# -- logic -- # declare vars
branches=0 branches=0
shoots=0 shoots=0
...@@ -364,6 +364,9 @@ init() { ...@@ -364,6 +364,9 @@ init() {
stty -echo # don't echo stdin 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 printf '%b' '\e[?25l\e[?7l\e[2J' # hide cursor, disable line wrapping, clear screen and move to 0,0
fi fi
# setup temp file for caching times of each growth
tmpTime="$(tempfile -p tmpTime)"
} }
grow() { grow() {
...@@ -578,15 +581,11 @@ branch() { ...@@ -578,15 +581,11 @@ branch() {
# print what we have so far # print what we have so far
if ((live)); then if ((live)); then
( time -p display ) 2>"$tmpTime"
begin="$(date +%s.%N)" # start timer elapsed="$(head "$tmpTime" -n 1 | awk '{print $2}' )"
display
# if this step took less than $stepTime, sleep until $stepTime is met # if this step took less than $stepTime, sleep until $stepTime is met
if ((live)); then timeLeft="$(printf '%s\n' "$timeStep - $elapsed" | bc -l)"
end="$(date +%s.%N)" # end timer [ "$(printf '%s\n' "($timeLeft) > 0" | bc -l)" -eq 1 ] && sleep "$timeLeft"
timeLeft="$(printf '%s\n' "($timeStep - ($end - $begin))" | bc -l)"
[ "$(printf '%s\n' "$timeLeft > 0" | bc -l)" -eq 1 ] && sleep "$timeLeft"
fi
fi fi
done done
} }
...@@ -601,7 +600,7 @@ display() { ...@@ -601,7 +600,7 @@ display() {
for item in "${changed[@]}"; do for item in "${changed[@]}"; do
if [ "$row" = "$item" ]; then if [ "$row" = "$item" ]; then
for (( col=0; col <= cols; col++ )); do 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 # grab the character from our grid
char="${grid[$row,$col]}" char="${grid[$row,$col]}"
...@@ -629,11 +628,12 @@ display() { ...@@ -629,11 +628,12 @@ display() {
quit() { quit() {
if ((! nfetch)); then 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 printf '%b\n' '\e[?25h\e[?7h'"${R}" # show cursor, enable line wrapping, reset colors
else else
printf '\n' # reset formatting, put cursor on next line printf '\n' # reset formatting, put cursor on next line
fi fi
rm "$tmpTime"
exit 0 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