Use BC_LINE_LENGTH=2 for bc.

It's not the case that macOS and old BC doesn't respect this environment
variable, just that they don't have special behavior when it's set to 0.
However, there is rather universal favorable behavior with a value of 2.

Output is of the form:
\
999999999999999999999999999999999...

with the second line being arbitrarily long. So just grab that line
instead of stitching with `string`.

This can yield a 25-30% speedup.
This commit is contained in:
Aaron Gyes 2016-12-29 03:42:08 -08:00
parent ee6691458e
commit 26e781ef5a

View file

@ -21,9 +21,12 @@ function math --description "Perform math calculations in bc"
return 2 # no arguments is an error return 2 # no arguments is an error
end end
# Stitch lines together manually. We can't rely on BC_LINE_LENGTH because some systems don't # With BC_LINE_LENGTH set to 2, the first line is a slash and the following line can be arbitrarily long.
# have a new enough version of bc. # We can't set this to 0 because some systems don't have a new enough version of bc.
set -l out (echo "scale=$scale; $argv" | bc | string replace -r '\\\\$' '' | string join '') set -lx BC_LINE_LENGTH 2
set -l out (echo "scale=$scale; $argv" | bc)
set -q out[2]
and set out $out[2]
switch "$out" switch "$out"
case '' case ''
# No output indicates an error occurred. # No output indicates an error occurred.