From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas Schwab Newsgroups: gmane.emacs.devel Subject: Re: why is reverse a string in-place so much slower than a vector? Date: Fri, 25 Apr 2014 11:14:11 +0200 Message-ID: <87zjj9n53w.fsf@igel.home> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1398417280 5215 80.91.229.3 (25 Apr 2014 09:14:40 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 25 Apr 2014 09:14:40 +0000 (UTC) Cc: emacs-devel@gnu.org To: Leo Liu Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Apr 25 11:14:33 2014 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WdcDI-0005os-Kz for ged-emacs-devel@m.gmane.org; Fri, 25 Apr 2014 11:14:32 +0200 Original-Received: from localhost ([::1]:56746 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WdcDI-0002lb-6n for ged-emacs-devel@m.gmane.org; Fri, 25 Apr 2014 05:14:32 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47077) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WdcD8-0002gH-UL for emacs-devel@gnu.org; Fri, 25 Apr 2014 05:14:29 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WdcD2-0006n5-JA for emacs-devel@gnu.org; Fri, 25 Apr 2014 05:14:22 -0400 Original-Received: from mail-out.m-online.net ([212.18.0.9]:40369) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WdcD2-0006ly-DP for emacs-devel@gnu.org; Fri, 25 Apr 2014 05:14:16 -0400 Original-Received: from frontend1.mail.m-online.net (unknown [192.168.8.180]) by mail-out.m-online.net (Postfix) with ESMTP id 3gFV7S3LJcz4KK5p; Fri, 25 Apr 2014 11:14:12 +0200 (CEST) Original-Received: from localhost (dynscan1.mnet-online.de [192.168.6.68]) by mail.m-online.net (Postfix) with ESMTP id 3gFV7S37MCzbcDb; Fri, 25 Apr 2014 11:14:12 +0200 (CEST) X-Virus-Scanned: amavisd-new at mnet-online.de Original-Received: from mail.mnet-online.de ([192.168.8.180]) by localhost (dynscan1.mail.m-online.net [192.168.6.68]) (amavisd-new, port 10024) with ESMTP id Xtysf5hVbYU3; Fri, 25 Apr 2014 11:14:11 +0200 (CEST) X-Auth-Info: aa0EKNEn6FmjqCFjvLoYkyzvE1Q9o7ZtmvldG87E5GY= Original-Received: from igel.home (host-188-174-222-231.customer.m-online.net [188.174.222.231]) by mail.mnet-online.de (Postfix) with ESMTPA; Fri, 25 Apr 2014 11:14:11 +0200 (CEST) Original-Received: by igel.home (Postfix, from userid 1000) id 73F3D2C1C7C; Fri, 25 Apr 2014 11:14:11 +0200 (CEST) X-Yow: The PINK SOCKS were ORIGINALLY from 1952!! But they went to MARS around 1953!! In-Reply-To: (Leo Liu's message of "Fri, 25 Apr 2014 16:17:42 +0800") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-Received-From: 212.18.0.9 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:171623 Archived-At: Leo Liu writes: > Assume we are in a buffer visiting subr.el: > > (benchmark-run 1 (rev (buffer-string))) > (11.774416366 1 0.060193897999999635) > > (benchmark-run 1 (rev (cl-coerce (buffer-string) 'vector))) > (0.067042623 0 0.0) > > So why is this so much slower on string? String random access has linear complexity: there is a single element cache for the last known char->byte mapping for the last accessed string, and the runtime depends on the distance from this point. Your rev function represents the worst case behaviour. Andreas. -- Andreas Schwab, schwab@linux-m68k.org GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."