Skip to content

command substitution vs read benchmarks #8542

Description

@IlanCosman

fish, version 3.3.1-701-gceade1629

set -g string (string repeat -n 1 h)
set -q list || set -U list (seq 500)

function command_substitution
    set -l foo
    for i in $list
        set foo (echo $string$i)
    end
end

function builtin_read_command
    set -l foo
    for i in $list
        echo $string$i | read foo
    end
end
ilan@arch ~> hyperfine --shell=fish --warmup=100 --runs=100 command_substitution builtin_read_command
Benchmark 1: command_substitution
  Time (mean ± σ):      53.1 ms ±   7.4 ms    [User: 44.2 ms, System: 18.0 ms]
  Range (min … max):    26.7 ms …  61.9 ms    100 runs
 
Benchmark 2: builtin_read_command
  Time (mean ± σ):      43.1 ms ±   2.4 ms    [User: 21.8 ms, System: 12.4 ms]
  Range (min … max):    27.5 ms …  46.6 ms    100 runs
 
Summary
  'builtin_read_command' ran
    1.23 ± 0.18 times faster than 'command_substitution'

read performance is highly dependant on string length however. If we set string like so:

set -g string (string repeat -n 100 h)

The following happens:

ilan@arch ~> hyperfine --shell=fish --warmup=100 --runs=100 command_substitution builtin_read_command
Benchmark 1: command_substitution
  Time (mean ± σ):      61.0 ms ±   8.7 ms    [User: 54.4 ms, System: 15.1 ms]
  Range (min … max):    30.3 ms …  67.9 ms    100 runs
 
Benchmark 2: builtin_read_command
  Time (mean ± σ):      86.3 ms ±   2.1 ms    [User: 34.7 ms, System: 42.0 ms]
  Range (min … max):    70.9 ms …  91.6 ms    100 runs
 
Summary
  'command_substitution' ran
    1.42 ± 0.20 times faster than 'builtin_read_command'

The equivalence point is around 32 on my machine.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePurely performance-related enhancement without any changes in black box outputquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions