Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16378

'while read' loop performance (redirection vs pipeline)

Path csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Terence O'Gorman <tog@aliquot.org>
Newsgroups gnu.bash.bug
Subject 'while read' loop performance (redirection vs pipeline)
Date Wed, 10 Jun 2020 12:48:52 +0100
Lines 80
Approved bug-bash@gnu.org
Message-ID <mailman.1581.1591793061.2541.bug-bash@gnu.org> (permalink)
References <1591789732.21528.14.camel@aliquot.org>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 7bit
X-Trace usenet.stanford.edu 1591793062 21397 209.51.188.17 (10 Jun 2020 12:44:22 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
X-Mailer Evolution 3.12.11 (3.12.11-1.fc21)
Received-SPF pass client-ip=85.233.160.19; envelope-from=tog@aliquot.org; helo=smtp.hosts.co.uk
X-detected-operating-system by eggs.gnu.org: First seen = 2020/06/10 07:48:53
X-ACL-Warn Detected OS = Linux 2.2.x-3.x (no timestamps) [generic]
X-Spam_score_int -25
X-Spam_score -2.6
X-Spam_bar --
X-Spam_report (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN
X-Spam_action no action
X-Mailman-Approved-At Wed, 10 Jun 2020 08:44:20 -0400
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <1591789732.21528.14.camel@aliquot.org>
Xref csiph.com gnu.bash.bug:16378

Show key headers only | View raw


Configuration Information:

Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -O2 -g -pipe -Wall -Werror=format-security
-Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions
-fstack-protector-strong -grecord-gcc-switches
-specs=/usr/lib/rpm/redhat/redhat-hardened-cc1
-specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic
-fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection
-Wno-parentheses -Wno-format-security
uname output: Linux quietpc 5.6.13-200.fc31.x86_64 #1 SMP
              Thu May 14 23:26:14 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-redhat-linux-gnu

Bash Version: 5.0
Patch Level: 11
Release Status: release


Description:

The 'while read' loop exhibits very different performance depending on
whether input is via redirection or pipeline (and increasingly diverging
performance with any increased data).  Here are some results from my
machine (also observed in versions 3.0 and 4.0):

[tog@quietpc ~]$ ~/loop.bash

100,000 lines of length 4:
'<': real = 0.609, user = 0.522, sys = 0.087
'|': real = 1.000, user = 0.561, sys = 0.793

100,000 lines of length 8:
'<': real = 0.652, user = 0.567, sys = 0.084
'|': real = 1.788, user = 0.772, sys = 1.838

100,000 lines of length 16:
'<': real = 0.660, user = 0.541, sys = 0.118
'|': real = 2.900, user = 1.019, sys = 3.389

100,000 lines of length 32:
'<': real = 0.684, user = 0.576, sys = 0.107
'|': real = 5.962, user = 1.560, sys = 7.809

100,000 lines of length 64:
'<': real =  0.763, user = 0.677, sys =  0.086
'|': real = 11.602, user = 2.803, sys = 15.727

100,000 lines of length 128:
'<': real =  0.984, user = 0.874, sys =  0.109
'|': real = 22.837, user = 5.061, sys = 31.592


Repeat-By:

#!/bin/bash

count=${1:-100000}
file=$(mktemp /tmp/loop.XXX)
string="####"

TIMEFORMAT="real = %R, user = %U, sys = %S"

for loop in {1..6}
  do
    printf "\n%'d lines of length %d:\n" $count ${#string}
    yes $string | sed ${count}q >$file

    printf "'<': "; time             while read line; do :; done <$file
    printf "'|': "; time cat $file | while read line; do :; done

    string+=$string
  done

rm -f $file


Back to gnu.bash.bug | Previous | Next | Find similar


Thread

'while read' loop performance (redirection vs pipeline) Terence O'Gorman <tog@aliquot.org> - 2020-06-10 12:48 +0100

csiph-web