On Tue, Jun 10, 2003 at 04:00:07PM -0700, Jonathan Sillito wrote: > > -----Original Message----- > > From: Jerome Vouillon [mailto:[EMAIL PROTECTED] > > > > The python interpreter seems rather slow. I get these numbers with the > > Ocaml bytecode interpreter. > > > > mistral-jerome:/tmp > time python test.py > > python test.py 2,59s user 0,00s system 100% cpu 2,582 total > > mistral-jerome:/tmp > ocamlc -o tst test.ml; time ./tst > > ./tst 0,14s user 0,00s system 106% cpu 0,131 total > > mistral-jerome:/tmp > cat test.ml > > let foo () = () in > > for i = 1 to 1000000 do foo () done > > That is impressive. I don't know Ocaml but do you think there is an > optimization being done since foo takes no args, returns not values and has > an empty body?
No, the bytecode compiler is not performing any optimization in this case. Here is the bytecode produced (the Ocaml virtual machine has an accumulator and a stack). -- Jerome 0 BRANCH 5 Jump to offset 5 (skip the function body). Function "foo" : 2 CONST0 Store 0 in the accumulator (this is the function result). 3 RETURN 1 Pop one element (the function argument). and return. 5 CLOSURE 0, 2 Build the "foo" function closure (empty environment, code at offset 2). Initialisation of the loop : 8 PUSHCONST1 Push the closure on the stack. and store 1 (the loop counter) in the accumulator. 9 PUSHCONSTINT 10000000 Push the value of the accumulator. and store 10000000 in the accumulator. 11 PUSH Push the value of the accumulator. 12 BRANCH 23 Jump to offset 23. The loop : 14 CHECK_SIGNALS Check whether there is any pending signal (Control-C, for instance) 15 CONST0 Store 0 in the accumulator (the function parameter). 16 PUSHACC3 Push the value of the accumulator and access the 4th element of the stack (the function closure). 17 APPLY1 Apply the function to its arguments. 18 ACC1 Store the 2nd element of the stack (the loop counter) in the accumulator. 19 OFFSETINT 1 Increment the loop counter. 21 ASSIGN 1 Store the value of the loop counter back in the stack. 23 ACC0 Store the content of the top of the stack (10000000) in the accumulator. 24 PUSHACC2 Push the value of the accumulator and store the 3rd element of the stack (the loop counter) in the accumulator. 25 LEINT Compare the accumulator and the top of the stack. The top of the stack is popped and the result is stored in the accumulator. 26 BRANCHIF 14 If the result is true, then jump to offset 14. Clean-up after the loop : 28 CONST0 Put 0 in the accumulator (return value of the loop) 29 POP 3 Pop all three elements of the stack