Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/5/84; site uwmacc.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxj!mhuxn!houxm!ihnp4!zehntel!hplabs!hao!seismo!uwvax!uwmacc!jiml
From: jiml@uwmacc.UUCP (Jim Leinweber)
Newsgroups: net.math.stat
Subject: Re: S seems incredibly slow...
Message-ID: <516@uwmacc.UUCP>
Date: Mon, 3-Dec-84 10:07:53 EST
Article-I.D.: uwmacc.516
Posted: Mon Dec  3 10:07:53 1984
Date-Received: Thu, 6-Dec-84 05:05:57 EST
References: <316@rna.UUCP> <42600001@uiucuxc.UUCP>
Reply-To: jiml@uwmacc.UUCP (Jim Leinweber)
Organization: UWisconsin-Madison Academic Comp Center
Lines: 34
Summary: One culprit is assign

On a lightly loaded VAX 11/780 running 4.2 BSD, with nearly everything
in the S executive,

	write(1:10000, "/tmp/junk")

took about 25 seconds of user+system time (2 minutes real); and a
shortly subsequent

	m <- matrix( read("/tmp/junk"),100,100)

took about 20 seconds of user+system time.  On the other hand,

	for (i in 1:20) for (j in 1:20) m[i,j] <- sin(j/10)

was so slow I didn't wait for it to finish.  I'm not an expert on the
bowels of S, but a cursory glance at $M/lang3.yr shows that each
occurence of `<-' invokes $F/assign, which in turn calls $L/getds,
$P/pcopy, and $L/putds.  Thus one problem with the nested for loop is
that assignment is very expensive in S; apparently each assignment
copies a dataset from one file to another!  Doing O(n^2) complete file
rewrites to initialize a matrix is *bound* to be slow.

Fortunately, S has extensive resources for avoiding loops. Two examples
that spring to mind, taking perhaps .3 and .1 seconds respectively:

    m <- matrix(0,20,20);    m <- sin( col(m)/10)

    m <- matrix( sin(1:20/10), 20,20, byrow=T)

			Jim Leinweber

UUCP:  ...!{allegra,ihnp4,seismo,...}!uwvax!uwmacc!jiml
ARPA:  uwmacc!jiml@wisc-rsch.arpa
POST:  MACC, UW-Madison, 1210 W. Dayton St., Madison, Wi, 53706