Tiny Core Linux
General TC => Programming & Scripting - Unofficial => Topic started by: Rich on May 24, 2026, 02:50:25 PM
-
Last year I was doing some network and timing testing, and
I created a few functions for my .ashrc file.
One of the functions is called Functions. Calling it from a
terminal lists the other functions:
tc@E310:~$ Functions
#: Calculate number of bytes received between calling GetRXD1 and GetRXD2.
GetRXD1() GetRXD2() CalcRXD()
#: Calculate number of bytes transmitted between calling GetTXD1 and GetTXD2.
GetTXD1() GetTXD2() CalcTXD()
#: Calculate elapsed time.
#: 10mSec resolution, but less overhead than $(date +%s.%3N).
GetET1() GetET2() CalcET()
After calling GetRXD1 and GetRXD2 , calling CalcRXD will subtract and display
the number of bytes received between those first two calls.
The TXD functions work the same way.
The ET functions do the same thing for time.
One of the reasons I created that one was because the time command
would complain about some of the things I wanted to time, like this:
tc@E310:~$ time for N in `seq 1 1000`; do RA=$(cat /sys/class/net/eth0/statistics/rx_bytes); done
sh: syntax error: unexpected "do"
I got tired of fighting it, so I made the timing functions:
tc@E310:~$ GetET1; for N in `seq 1 1000`; do RA=$(cat /sys/class/net/eth0/statistics/rx_bytes); done; GetET2; CalcET
1.55
tc@E310:~$ GetET1; for N in `seq 1 1000`; do RA=$(cat /sys/class/net/eth0/statistics/rx_bytes); done; GetET2; CalcET
1.54
tc@E310:~$ GetET1; for N in `seq 1 1000`; do RA=$(cat /sys/class/net/eth0/statistics/rx_bytes); done; GetET2; CalcET
1.54
tc@E310:~$ GetET1; for N in `seq 1 1000`; do RA=$(cat /sys/class/net/eth0/statistics/rx_bytes); done; GetET2; CalcET
1.53
Why would I want to do that? Because I wanted to see the speed difference
between using cat and read to retrieve a value:
tc@E310:~$ GetET1; for N in `seq 1 1000`; do while read line; do RA=$line; done </sys/class/net/eth0/statistics/rx_bytes; done; GetET2; CalcET
0.18
tc@E310:~$ GetET1; for N in `seq 1 1000`; do while read line; do RA=$line; done </sys/class/net/eth0/statistics/rx_bytes; done; GetET2; CalcET
0.18
tc@E310:~$ GetET1; for N in `seq 1 1000`; do while read line; do RA=$line; done </sys/class/net/eth0/statistics/rx_bytes; done; GetET2; CalcET
0.18
tc@E310:~$ GetET1; for N in `seq 1 1000`; do while read line; do RA=$line; done </sys/class/net/eth0/statistics/rx_bytes; done; GetET2; CalcET
0.19
Or this slightly simplified version using read:
tc@E310:~$ GetET1; for N in `seq 1 1000`; do read -r RA </sys/class/net/eth0/statistics/rx_bytes; done; GetET2; CalcET
0.17
tc@E310:~$ GetET1; for N in `seq 1 1000`; do read -r RA </sys/class/net/eth0/statistics/rx_bytes; done; GetET2; CalcET
0.17
tc@E310:~$ GetET1; for N in `seq 1 1000`; do read -r RA </sys/class/net/eth0/statistics/rx_bytes; done; GetET2; CalcET
0.16
tc@E310:~$ GetET1; for N in `seq 1 1000`; do read -r RA </sys/class/net/eth0/statistics/rx_bytes; done; GetET2; CalcET
0.17
So this is what I added to the end of my .ashrc file:
# Lists the descriptions of the functions defined below.
Functions(){
while read -r LINE
do
if [ "${LINE:0:2}" == "#:" ]
then
echo "$LINE"
Funcs=" "
while [ "$LINE" != "" ]
do
read -r LINE
[ "${LINE:0:2}" == "#:" ] && echo "$LINE" && continue
# Skip commented lines.
[ "${LINE:0:1}" == "#" ] && [ "${LINE:1:1}" != ":" ] && continue
Funcs="$Funcs $(echo $LINE | cut -d '{' -f1)"
done
echo -e "$Funcs\n"
fi
done < "$HOME/.ashrc"
}
#: Calculate number of bytes received between calling GetRXD1 and GetRXD2.
# File contains one number, so loop executes once. Much faster than cat.
GetRXD1(){ while read line; do RXD1=$line; done <"$STATS"/rx_bytes; }
GetRXD2(){ while read line; do RXD2=$line; done <"$STATS"/rx_bytes; }
CalcRXD(){ calc $RXD2-$RXD1; }
#: Calculate number of bytes transmitted between calling GetTXD1 and GetTXD2.
GetTXD1(){ while read line; do TXD1=$line; done <"$STATS"/tx_bytes; }
GetTXD2(){ while read line; do TXD2=$line; done <"$STATS"/tx_bytes; }
CalcTXD(){ calc $TXD2-$TXD1; }
#: Calculate elapsed time.
#: 10mSec resolution, but less overhead than GNU $(date +%s.%3N).
# This also appears more repeatable. "while read" is faster than cat.
GetET1(){ while read Time; do ET1=$Time; done </proc/uptime; }
GetET2(){ while read Time; do ET2=$Time; done </proc/uptime; }
CalcET(){ calc ${ET2%% *}-${ET1%% *}; }