Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Eric Blake Newsgroups: gnu.bash.bug Subject: Re: Performance bug of {1..1000000}? Date: Mon, 9 Mar 2020 11:41:05 -0500 Organization: Red Hat, Inc. Lines: 32 Approved: bug-bash@gnu.org Message-ID: References: NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: usenet.stanford.edu 1583772075 14336 209.51.188.17 (9 Mar 2020 16:41:15 GMT) X-Complaints-To: action@cs.stanford.edu To: Peng Yu , bug-bash@gnu.org Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583772071; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PDYs1aynh7xmfYvFtmK4jrRl7+DDHw0WcgMNl2gLkT0=; b=R/YvG8iRLiPN7tPeBBTGjegbwwHLkw50r5DTKqU5E0lPtykMB3rSe8JJA/j8q1nUBua+Xm kTeFBfTv5hienQpsUoq2aCFAKSxPVXgqherYcpqTTIDAEqATPzoudlVmJkULs6wVNmC4yt 59hhigM8sTN7f9whEmhJ3zrsr3VK5gs= X-MC-Unique: raK5mMy-MoukjTcBg_qs6Q-1 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 In-Reply-To: Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com gnu.bash.bug:16004 On 3/7/20 10:39 AM, Peng Yu wrote: > See the following run time comparison. {1..1000000} is slower than > $(seq 1000000). > > Since seq involves an external program, I'd expect the latter to be > slower. But the comparison shows the opposite. > > I guess seq did some optimization? seq does not have to store the entire sequence in memory. As it outputs things to stdout (and the other end of the pipeline consumes that output), seq can forget what it has previously done. Bash, on the other hand, computes the entire expansion in memory prior to proceeding to use that expansion. It is the cost of memory allocation and increased memory usage that slows bash down. > > Can the performance of {1..1000000} be improved so that it is faster > than $(seq 1000000)? Not without someone writing a patch. Are you volunteering? But in general, we don't recommend trying to make bash do expansions like that, when it is already more efficient to use other means of iteration that do not require bash to keep the entire sequence in memory. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org