Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!217.188.199.168.MISMATCH!takemy.news.telefonica.de!telefonica.de!feed.news.schlund.de!schlund.de!news.online.de!not-for-mail From: Bernd Paysan Newsgroups: comp.lang.forth Subject: Re: Forth vs Java in calculating partial sum ;) Date: Sat, 12 Apr 2014 22:50:50 +0200 Organization: 1&1 Internet AG Lines: 53 Message-ID: References: NNTP-Posting-Host: p4ffb9415.dip0.t-ipconnect.de Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: online.de 1397335851 14308 79.251.148.21 (12 Apr 2014 20:50:51 GMT) X-Complaints-To: abuse@einsundeins.com NNTP-Posting-Date: Sat, 12 Apr 2014 20:50:51 +0000 (UTC) User-Agent: KNode/4.11.5 Xref: csiph.com comp.lang.forth:29593 mhx@iae.nl wrote: > On Saturday, April 12, 2014 7:11:11 PM UTC+2, Melzzzzz wrote: >> Seems that Java bits Forth in optimizing tight loops. Eg division by >> constant optimization is performed automatically. > > [..] > > Paraphrased: > > ANEW -cumul > > #50000000 =: N > N CELLS ALLOCATE ?ALLOCATE =: addr > > : init_cumul ( -- ) addr N 0 ?DO I 3 / I + SWAP !+ LOOP DROP ; > > : cumul ( -- ) > addr @+ ( [a] a -- ) > N 1 ?DO > OVER @ + DUP ( a [a]+x [a]+x -- ) > ROT !+ ( [a]+x a2 -- ) SWAP > LOOP 2DROP ; > > : bench ( xt -- ) > TIMER-RESET > EXECUTE > MS? S>F 1e-3 F* N S>F F/ CR F.N1 ." s/op, " .ELAPSED ; > > ' init_cumul bench > ' cumul bench > CR addr N 1- CELL[] @ . > addr FREE ?ALLOCATE > > \ 3.68 ns/op, 0.186 seconds elapsed. > \ 2.16 ns/op, 0.109 seconds elapsed. > \ 1666666616666667 ok > > I guess Java programmers are paid per LOC. That's how I would write cumul (with the original stack effeckt, which Melzzzzz forgot to write): : cumul ( addr -- addr ) dup N 1- cells bounds ?do i @ i cell+ +! cell +loop ; -- Bernd Paysan "If you want it done right, you have to do it yourself" http://bernd-paysan.de/