Skip to content

math command is extremely slow -- it should be a builtin #3157

@jichu4n

Description

@jichu4n

The math builtin in fish is extremely slow (~30x slower than bash arithmetic).

Reproduction Steps:

  1. Run the math builtin for simple arithmetic (tested on fish 2.3.0):

    $ time -p fish -c 'for i in (seq 100); math "$i * 100000" > /dev/null; end'
    
  2. Test bash's builtin arithmetic:

    $ time -p bash -c 'for i in $(seq 100); do echo $(( $i * 100000 )) > /dev/null; done'
    

Expected behavior:

The execution time of the math builtin should be on the same order of magnitude as bash arithmetic.

Observed behavior:

The math builtin is ~30x slower than bash arithmetic for the simple test case above, as measured on a Raspberry Pi B+:

$ time -p fish -c 'for i in (seq 100); math "$i * 100000" > /dev/null; end'
real 5.25
user 1.98
sys 1.49

$ time -p bash -c 'for i in $(seq 100); do echo $(( $i * 100000 )) > /dev/null; done'
real 0.16
user 0.06
sys 0.00

Additional information:

I ran into this problem when developing the fish-command-timer script, which makes multiple consecutive calls to math to breakdown a timestamp difference into hours, minutes and seconds. On slower hardware such as the Raspberry Pi, the multiple math invocations take ~1s to execute, which renders the experience painful. There aren't any alternatives to the math builtin without additional dependencies.


Fish version: 2.3.0

Operating system: Arch Linux ARM for Raspberry Pi

Terminal or terminal emulator: MATE Terminal 1.12.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions