2

Why does running the bash history builtin function in a subshell run 56x slower?

(I have 22k lines in my history. Maybe that is significant?)

$ time history > /dev/null

real    0m0.064s
user    0m0.049s
sys     0m0.015s

$ # in a subshell it runs 56x slower:
$ time (history > /dev/null)

real    0m4.780s
user    0m2.558s
sys     0m2.215s

$ # trying "date" instead to show "time" isn't the issue:

$ date; history > /dev/null; date
Wed Oct 21 21:50:51 PDT 2020
Wed Oct 21 21:50:51 PDT 2020  (< 1 sec)

$ date; (history > /dev/null); date
Wed Oct 21 21:50:51 PDT 2020
Wed Oct 21 21:50:56 PDT 2020  (5 secs)

$ # each command in a pipeline is executed in a subshell (bash(1))
$ date; history | tail -1 > /dev/null; date
Wed Oct 21 21:50:56 PDT 2020
Wed Oct 21 21:51:01 PDT 2020  (5 secs)

$ history|wc -l
   22267

$ # time for a simple command ("date")
$ time date
Sat Jul  2 17:40:33 PDT 2022

real    0m0.004s
user    0m0.001s
sys     0m0.003s

$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin15.6.0)

Mac OS version: macOS Mojave 10.14.6

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
  • Mystery. It's not slower for me. (But I have a limit of 2000 lines.) – Rick James Oct 22 '20 at 05:14
  • Interesting but i can't reproduse on `Linux Mint 20`, `bash 5.0.17(1)-release (x86_64-pc-linux-gnu)` test: time history > /dev/null real 0m0,001s user 0m0,001s sys 0m0,000s time (history > /dev/null) real 0m0,001s user 0m0,001s sys 0m0,000s – Ivan Oct 22 '20 at 05:17
  • I have 22k lines of history. Maybe that is a deciding factor. – Rob Bednark Oct 22 '20 at 05:35
  • 3
    Stack Overflow is for programming questions. You might try asking on [Unix & Linux](https://unix.stackexchange.com/) or [Super User](https://superuser.com/) instead. – John Kugelman Oct 22 '20 at 05:36
  • @RobBednark : Can't reproduce on Cygwin. – user1934428 Oct 22 '20 at 06:20
  • 1
    Can't reproduce with 1670-line ~/.bash_history. Bash 5.0.17(1)-release on Ubuntu 20.04. But this only shows one run. If you had consistent behaviour over *many* interleaved executions that might be more telling. Also, part of the runtime (maybe a huge part) is from starting the subshell. Have you tried with a no-op command like `true`? – l0b0 Oct 22 '20 at 06:22

0 Answers0