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


Groups > comp.lang.forth > #4638

Re: Forth Performance Question

From stephenXXX@mpeforth.com (Stephen Pelc)
Newsgroups comp.lang.forth
Subject Re: Forth Performance Question
Message-ID <4e3e7776.231242091@192.168.0.50> (permalink)
References <f0fc528e-2fdb-42af-a66d-976ebfa73b88@c8g2000prn.googlegroups.com>
Date 2011-08-07 11:46 +0000

Show all headers | View raw


On Sat, 6 Aug 2011 20:43:22 -0700 (PDT), TS <thinksquared@gmail.com>
wrote:

>: foo  1 1000000 * drop ;
>: test 1 100000000 ?DO foo LOOP ;

That should be
  : test 100000000 1 ?DO foo LOOP ;

>On BigForth 2.4.0 it took ~22 sec to run, Retro v11 (with "times"
>syntax, compiled with -O3), took ~23 sec and GForth 0.7.0 ~65 sec to
>run.
>
>I also tried to compare these numbers with Java (Sun, 1.6), assuming
>that since it also uses a stack based VM, I would get comparable
>performance:
>
>public class Test {
>	public static void main(String[] args) {
>		for(int i = 0; i < 100000000; ++i){
>			foo();
>		}
>	}
>	private static final int foo(){
>		return 1*1000000;
>	}
>}
>
>Which did it in less than a second (!). I've tried variations on the
>Java program (eg, creating a local variable x to act as a counter
>instead of calling foo(), and the timing is always still less than a
>second.
>
>I'm using Ubuntu  (64 bit).and GCC 4.4.3 on an Intel Core2 Duo laptop.
>
>I have two main questions:
>
>a) Since BigForth is compiles to native code, why is its performance
>similar to Retro, which is interpreted?
>
>b) Why the enormous difference in speed between Java (which is also
>based on a stack VM) and these Forths? Is it because the JIT performs
>inlining? Or might there be other reasons?

First, let's normalise so that Forth is doing what was intended. This
is VFX Forth for Windows on a 2.0GHz Core2 Duo.

: foo  1 1000000 * drop ;  ok
: test 1 100000000 ?DO foo LOOP ;  ok
test  ok
dis test 
TEST 
( 004C6DF0    681F6E4C00 )            PUSH      004C6E1F
( 004C6DF5    68FFE0F585 )            PUSH      85F5E0FF
( 004C6DFA    6800E1F505 )            PUSH      05F5E100
( 004C6DFF    817C240400000080 )      CMP       [ESP+04], 80000000
( 004C6E07    7505 )                  JNZ/NE    004C6E0E
( 004C6E09    8D642408 )              LEA       ESP, [ESP+08]
( 004C6E0D    C3 )                    NEXT,
( 004C6E0E    90 )                    NOP
( 004C6E0F    90 )                    NOP
( 004C6E10    83042401 )              ADD       [ESP], 01
( 004C6E14    8344240401 )            ADD       [ESP+04], 01
( 004C6E19    71F5 )                  JNO       004C6E10
( 004C6E1B    8D64240C )              LEA       ESP, [ESP+0C]
( 004C6E1F    C3 )                    NEXT,
( 48 bytes, 14 instructions )
 ok
timer-reset test .elapsed 12761 ms elapsed ok

The compiler noted that FOO does nothing, generated a compile-time
literal, and discarded it at compile time.

dis foo 
FOO 
( 004C6DD0    C3 )                    NEXT,
( 1 bytes, 1 instructions )

The tokeniser is still active, so nothing was compiled when FOO
was compiled.

That was running the loop the wrong way. Now change it.

: test 100000000 1 ?DO foo LOOP ; 
TEST is redefined  ok
timer-reset test .elapsed 359 ms elapsed ok

Note that DO ... LOOP affects the return stack and is not necessarily
the fastest counted loop.

: test2  100000000 begin  foo  1- dup 0= until drop ;  ok
dis test2 
TEST2 
( 004C6E40    8D6DFC )                LEA       EBP, [EBP+-04]
( 004C6E43    895D00 )                MOV       [EBP], EBX
( 004C6E46    BB00E1F505 )            MOV       EBX, 05F5E100
( 004C6E4B    90 )                    NOP
( 004C6E4C    90 )                    NOP
( 004C6E4D    90 )                    NOP
( 004C6E4E    90 )                    NOP
( 004C6E4F    90 )                    NOP
( 004C6E50    4B )                    DEC       EBX
( 004C6E51    85DB )                  TEST      EBX, EBX
( 004C6E53    75FB )                  JNZ/NE    004C6E50
( 004C6E55    8B5D00 )                MOV       EBX, [EBP]
( 004C6E58    8D6D04 )                LEA       EBP, [EBP+04]
( 004C6E5B    C3 )                    NEXT,
( 28 bytes, 14 instructions )
 ok
timer-reset test2 .elapsed 78 ms elapsed ok

The loop activity now happens entirely in registers and saves four
memory accesses.

Stephen


-- 
Stephen Pelc, stephenXXX@mpeforth.com
MicroProcessor Engineering Ltd - More Real, Less Time
133 Hill Lane, Southampton SO15 5AF, England
tel: +44 (0)23 8063 1441, fax: +44 (0)23 8033 9691
web: http://www.mpeforth.com - free VFX Forth downloads

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Forth Performance Question TS <thinksquared@gmail.com> - 2011-08-06 20:43 -0700
  Re: Forth Performance Question "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2011-08-07 01:02 -0400
    Re: Forth Performance Question Chris Hinsley <chris.hinsley@gmail.com> - 2011-08-16 12:09 +0100
      Re: Forth Performance Question stephenXXX@mpeforth.com (Stephen Pelc) - 2011-08-16 14:08 +0000
  Re: Forth Performance Question mhx@iae.nl (Marcel Hendrix) - 2011-08-07 07:45 +0200
    Re: Forth Performance Question anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2011-08-07 16:18 +0000
  Re: Forth Performance Question Bruno Gauthier <bgauthier@free.fr> - 2011-08-07 07:53 +0200
  Re: Forth Performance Question Julian Fondren <ayrnieu@gmail.com> - 2011-08-07 01:01 -0500
  Re: Forth Performance Question Albert van der Horst <albert@spenarnc.xs4all.nl> - 2011-08-07 12:50 +0000
  Re: Forth Performance Question stephenXXX@mpeforth.com (Stephen Pelc) - 2011-08-07 11:46 +0000
  Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-10 17:03 +0000
    Re: Forth Performance Question Julian Fondren <ayrnieu@gmail.com> - 2011-08-10 13:35 -0500
      Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-11 15:05 +0000
        Re: Forth Performance Question anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2011-08-11 16:26 +0000
          Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-12 08:15 +0000
            Re: Forth Performance Question "Elizabeth D. Rather" <erather@forth.com> - 2011-08-11 22:29 -1000
              Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-12 10:09 +0000
                Re: Forth Performance Question Julian Fondren <ayrnieu@gmail.com> - 2011-08-12 08:15 -0500
            Re: Forth Performance Question anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2011-08-12 09:31 +0000
        Re: Forth Performance Question crc <charles.childers@gmail.com> - 2011-08-11 10:27 -0700
        Re: Forth Performance Question Julian Fondren <ayrnieu@gmail.com> - 2011-08-11 13:18 -0500
        Re: Forth Performance Question "Elizabeth D. Rather" <erather@forth.com> - 2011-08-11 12:00 -1000
          Re: Forth Performance Question Howerd <howerdo@yahoo.co.uk> - 2011-08-11 15:13 -0700
          Re: Forth Performance Question Charles Childers <crc_nospam@retroforth.org> - 2011-08-11 20:52 -0400
          Re: Forth Performance Question "Rod Pemberton" <do_not_have@noavailemail.cmm> - 2011-08-12 02:19 -0400
            Re: Forth Performance Question Julian Fondren <ayrnieu@gmail.com> - 2011-08-12 02:10 -0500
              Re: Forth Performance Question "Elizabeth D. Rather" <erather@forth.com> - 2011-08-11 21:48 -1000
            Re: Forth Performance Question "Elizabeth D. Rather" <erather@forth.com> - 2011-08-11 21:41 -1000
    Re: Forth Performance Question Charles Childers <crc_nospam@retroforth.org> - 2011-08-10 23:33 -0400
      Re: Forth Performance Question Ron Aaron <rambamist@gmail.com> - 2011-08-11 08:59 +0300
      Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-11 13:48 +0000
        Re: Forth Performance Question Charles Childers <crc@retroforth.org> - 2011-08-11 10:30 -0400
    Re: Forth Performance Question Ron Aaron <rambamist@gmail.com> - 2011-08-11 08:46 +0300
    Re: Forth Performance Question arc <arc@vorsicht-bissig.de> - 2011-08-12 12:20 +0000
      Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-12 13:59 +0000
        Re: Forth Performance Question stephenXXX@mpeforth.com (Stephen Pelc) - 2011-08-12 15:11 +0000
          Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-12 17:49 +0000
            Re: Forth Performance Question stephenXXX@mpeforth.com (Stephen Pelc) - 2011-08-12 19:38 +0000
            Re: Forth Performance Question "Elizabeth D. Rather" <erather@forth.com> - 2011-08-12 12:41 -1000
              Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-13 03:35 +0000
                Re: Forth Performance Question "Elizabeth D. Rather" <erather@forth.com> - 2011-08-12 17:52 -1000
                Re: Forth Performance Question Paul Rubin <no.email@nospam.invalid> - 2011-08-12 23:55 -0700
                Re: Forth Performance Question Albert van der Horst <albert@spenarnc.xs4all.nl> - 2011-08-14 09:01 +0000
                Re: Forth Performance Question Paul Rubin <no.email@nospam.invalid> - 2011-08-14 01:36 -0700
                Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-15 01:43 +0000
                Re: Forth Performance Question Hugh Aguilar <hughaguilar96@yahoo.com> - 2011-08-15 16:59 -0700
                Re: Forth Performance Question Mark Wills <markrobertwills@yahoo.co.uk> - 2011-08-16 03:25 -0700
                Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-16 11:22 +0000
                Re: Forth Performance Question Hugh Aguilar <hughaguilar96@yahoo.com> - 2011-08-16 14:37 -0700
                Re: Forth Performance Question Arnold Doray <thinksquared@gmail.com> - 2011-08-19 14:11 +0000
                Re: Forth Performance Question Hugh Aguilar <hughaguilar96@yahoo.com> - 2011-08-22 19:52 -0700
            Re: Forth Performance Question Zbiggy <zbigniew2011REMOVE@gmail.REMOVE.com> - 2011-08-13 02:28 +0200
        Re: Forth Performance Question Zbiggy <zbigniew2011REMOVE@gmail.REMOVE.com> - 2011-08-12 20:53 +0200

csiph-web