Use bc in backround for multiple bc's running much faster

Use bc in backround for multiple bc's running much faster
This commit is contained in:
olli 2023-11-03 14:46:14 +01:00
parent 3ade97eea1
commit 301bcde417

View File

@ -1,3 +1,34 @@
function g_calc { function g_calc {
echo "scale=8; $@" | bc | sed 's/^\./0./; s/^-\./-0./'
} # Use bc in backround for multiple bc's running much faster
if [ -z "${g_bc_running}" ]
then
[ -p ${g_tmp}/bc-in ] || mkfifo ${g_tmp}/bc-in
[ -p ${g_tmp}/bc-out ] || mkfifo ${g_tmp}/bc-out
[ -p ${g_tmp}/bc-sed-in ] || mkfifo ${g_tmp}/bc-sed-in
[ -p ${g_tmp}/bc-sed-out ] || mkfifo ${g_tmp}/bc-sed-out
# bc stream channel
bc -ql <${g_tmp}/bc-in >${g_tmp}/bc-out &
exec 3>${g_tmp}/bc-in 4<${g_tmp}/bc-out
# sed stream channel
sed -su 's/^\./0./; s/^-\./-0./' <${g_tmp}/bc-sed-in >${g_tmp}/bc-sed-out &
exec 5>${g_tmp}/bc-sed-in 6<${g_tmp}/bc-sed-out
g_bc_running="true"
fi 2>/dev/null
# do bc
echo "scale=8; $@" >&3
local g_bc_out
read g_bc_out <&4
# do sed
echo ${g_bc_out} >&5
local g_sed_out
read g_sed_out <&6
# echo result
echo ${g_sed_out}
}