Array Access Back to the Win32 Shootout
Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]  
All Source For Array Access
ary3.awka
# $Id: ary3.gawk,v 1.1 2001/05/31 02:27:48 doug Exp $
# http://www.bagley.org/~doug/shootout/

# this program modified from:
#   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
# Timing Trials, or, the Trials of Timing: Experiments with Scripting
# and User-Interface Languages</a> by Brian W. Kernighan and
# Christopher J. Van Wyk.

BEGIN {
    n = (ARGV[1] < 1) ? 1 : ARGV[1];

    for (i = 0; i < n; i++)
    x[i] = i + 1
    for (k = 0; k < 1000; k++) {
    for (j = n-1; j >= 0; j--)
        y[j] += x[j]
    }

    print y[0], y[n-1]
}
ary3.bcc
/* -*- mode: c -*-
 * $Id: ary3.gcc,v 1.1 2001/05/31 02:27:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *
 * this program is modified from:
 *   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
 * Timing Trials, or, the Trials of Timing: Experiments with Scripting
 * and User-Interface Languages</a> by Brian W. Kernighan and
 * Christopher J. Van Wyk.
 *
 * I added free() to deallocate memory.
 */

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, k, *x, *y;
    
    x = (int *) calloc(n, sizeof(int));
    y = (int *) calloc(n, sizeof(int));

    for (i = 0; i < n; i++) {
    x[i] = i + 1;
    }
    for (k=0; k<1000; k++) {
    for (i = n-1; i >= 0; i--) {
        y[i] += x[i];
    }
    }

    fprintf(stdout, "%d %d\n", y[0], y[n-1]);

    free(x);
    free(y);

    return(0);
}
ary3.bigforth
\ $Id: ary3.bigforth,v 1.1 2001/06/19 16:20:45 doug Exp $
\ http://www.bagley.org/~doug/shootout/

decimal

\ read NUM from last command line argument
0. argc @ 1- arg >number 2drop drop constant NUM

variable X
NUM cells allocate drop X !
variable Y
NUM cells allocate drop Y !

: ary 
  NUM 0 do
    1 i + i cells X @ + !
  loop
  1000 0 do
    NUM 0 do
      i cells Y @ +       
      dup @
      i cells X @ +       
      @ + swap !
    loop
  loop ;

ary

Y @ @ 1 u.r ."  " NUM 1 - cells Y @ + @ 1 u.r cr

bye \ th-th-that's all folks!
ary3.csharp
// $Id: ary3.csharp,v 1.0 2002/05/09 12:57:00 dada Exp $
// http://dada.perl.it/shootout/

using System;

class App {

    public static int Main(String[] args) {        
        int i, j, k, n;
        int[] x;
        int[] y;
        
        n = System.Convert.ToInt32(args[0]);
        if(n < 1) n = 1;
        
        x = new int[n];
        y = new int[n];
        
        for (i = 0; i < n; i++)
            x[i] = i + 1;
        for (k = 0; k < 1000; k++ )
            for (j = n-1; j >= 0; j--)
                y[j] += x[j];
        
        Console.WriteLine(y[0].ToString() + " " + y[n-1].ToString());
        return(0);
    }
}
ary3.cygperl
#!/usr/local/bin/perl
# $Id: ary3.perl,v 1.1 2001/05/31 02:27:48 doug Exp $
# http://www.bagley.org/~doug/shootout/

# this program is modified from:
#   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
# Timing Trials, or, the Trials of Timing: Experiments with Scripting
# and User-Interface Languages</a> by Brian W. Kernighan and
# Christopher J. Van Wyk.

my $n = @ARGV[0] || 1;
my @X;
my @Y;

my $last = $n - 1;
for my $i (0..$last) {
    $X[$i] = $i + 1;
}
for my $k (0..999) {
    for my $i (reverse 0..$last) {
    $Y[$i] += $X[$i];
    }
}

print "$Y[0] $Y[$last]\n";
ary3.erlang
%%% -*- mode: erlang -*-
%%% $Id: ary3.erlang,v 1.3 2001/06/18 18:39:33 doug Exp $
%%% http://www.bagley.org/~doug/shootout/
%%% from James Hague

-module(ary3).
-export([main/1]).

main() -> main(['1']).
main([Arg]) ->
    Num = list_to_integer(atom_to_list(Arg)),
    ets:new(y, [set, private, named_table]),
    clear_y_array(Num),
    X = list_to_tuple(lists:seq(1, Num)),
    repeat(X, Num, 1000),
    [{_,First}] = ets:lookup(y, 1),
    [{_,Last}]  = ets:lookup(y, Num),
    io:fwrite("~w ~w~n", [First, Last]),
    ets:delete(y),
    halt(0).

clear_y_array(0) -> ok;
clear_y_array(I) ->
    ets:insert(y, {I,0}),
    clear_y_array(I - 1).

repeat(X, N, 0) -> ok;
repeat(X, N, K) ->
    calc(X, N),
    repeat(X, N, K - 1).

calc(X, 0) -> ok;
calc(X, N) ->
    ets:update_counter(y, N, element(N, X)),
    calc(X, N - 1).
ary3.fpascal
Program ary3;
uses SysUtils, Classes;

var
    n, i, k, last : longint;
    X, Y : TList;
begin
    if ParamCount = 0 then
        n := 1
    else
        n := StrToInt(ParamStr(1));
        
    if n < 1 then n := 1;
    
    last := n - 1;
    X := TList.Create;
    X.Capacity := n;
    For i := 0 To last do
        X.Add( Pointer(i+1) );
    
    Y := TList.Create;
    Y.Capacity := n;
    For i := 0 To last do
        Y.Add( Pointer(0) );
    
    
    For k := 0 To 999 do
    begin
        For i := last downto 0 do
        begin
            Y.Items[i] := Pointer(longint(Y.Items[i]) + longint(X.Items[i]));
        end;
    end;
    Writeln (IntToStr(longint(Y.Items[0])), ' ', IntToStr(longint(Y.Items[last])));
end.
ary3.gawk
# $Id: ary3.gawk,v 1.1 2001/05/31 02:27:48 doug Exp $
# http://www.bagley.org/~doug/shootout/

# this program modified from:
#   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
# Timing Trials, or, the Trials of Timing: Experiments with Scripting
# and User-Interface Languages</a> by Brian W. Kernighan and
# Christopher J. Van Wyk.

BEGIN {
    n = (ARGV[1] < 1) ? 1 : ARGV[1];

    for (i = 0; i < n; i++)
    x[i] = i + 1
    for (k = 0; k < 1000; k++) {
    for (j = n-1; j >= 0; j--)
        y[j] += x[j]
    }

    print y[0], y[n-1]
}
ary3.gcc
/* -*- mode: c -*-
 * $Id: ary3.gcc,v 1.1 2001/05/31 02:27:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *
 * this program is modified from:
 *   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
 * Timing Trials, or, the Trials of Timing: Experiments with Scripting
 * and User-Interface Languages</a> by Brian W. Kernighan and
 * Christopher J. Van Wyk.
 *
 * I added free() to deallocate memory.
 */

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, k, *x, *y;
    
    x = (int *) calloc(n, sizeof(int));
    y = (int *) calloc(n, sizeof(int));

    for (i = 0; i < n; i++) {
    x[i] = i + 1;
    }
    for (k=0; k<1000; k++) {
    for (i = n-1; i >= 0; i--) {
        y[i] += x[i];
    }
    }

    fprintf(stdout, "%d %d\n", y[0], y[n-1]);

    free(x);
    free(y);

    return(0);
}
ary3.gforth
\ $Id: ary3.gforth,v 1.1 2001/05/31 02:27:48 doug Exp $
\ http://www.bagley.org/~doug/shootout/

decimal

\ read NUM from last command line argument
0. argc @ 1- arg >number 2drop drop constant NUM

variable X
NUM cells allocate drop X !
variable Y
NUM cells allocate drop Y !

: ary 
  NUM 0 do
    1 i + i cells X @ + !
  loop
  1000 0 do
    NUM 0 do
      i cells Y @ +       
      dup @
      i cells X @ +       
      @ + swap !
    loop
  loop ;

ary

Y @ @ 1 u.r ."  " NUM 1 - cells Y @ + @ 1 u.r cr

bye \ th-th-that's all folks!
ary3.ghc
-- $Id: ary3.ghc,v 1.2 2001/06/01 17:56:49 doug Exp $
-- http://www.bagley.org/~doug/shootout/

-- TBD: try rewrite with STUArray or IOUArray

module Main where

import IArray
import System

make_x :: Int -> UArray Int Int
make_x len = array (1,len) [(i, i) | i <- [1..len]]

make_y0 :: Int -> UArray Int Int
make_y0 len = array (1,len) [(i, 0) | i <- [1..len]]

add_array :: Int -> UArray Int Int -> UArray Int Int -> UArray Int Int
add_array 0 a b = b
add_array len a b = array (1,len) [(i, ((a ! i) + (b ! i))) | i <- [1..len]]

add_arrays_n :: Int -> Int -> UArray Int Int -> UArray Int Int -> UArray Int Int
add_arrays_n 0 len a b = b
add_arrays_n n len a b =
    add_arrays_n (n-1) len a (add_array len a b)

ary3 :: Int -> IO ()
ary3 len = do putStr (show (y ! 1)) ; putStr " " ; putStrLn (show (y ! len))
             where y = add_arrays_n 1000 len (make_x len) (make_y0 len)

main = do
        ~[n] <- getArgs 
        ary3 (read n::Int)
ary3.gnat
-- $Id: ary3.gnat,v 1.0 2003/06/11 12:08:00 dada Exp $
-- http://dada.perl.it/shootout/
-- Ada 95 code by C.C.

with Text_IO, Ada.Command_Line, Ada.Strings.Fixed;

procedure Ary3 is
   function L_Trim (Source : String; Side : Ada.Strings.Trim_End :=
               Ada.Strings.Left) return String renames Ada.Strings.Fixed.Trim;
   N        : Positive := 1;
begin
   begin
      N := Positive'Value (Ada.Command_Line.Argument (1));
   exception
      when Constraint_Error => null;
   end;
   declare
      type Vect is array (1 .. N) of Integer;
      X, Y     : Vect;
   begin
      for K in Vect'Range loop
         X (K) := K;
         Y (K) := 0;
      end loop;

      for Iter in 1 .. 1000 loop
         for K in reverse Vect'Range loop
            declare
               Y_K      : Integer renames Y (K);
            begin
               Y_K := Y_K + X (K);
            end;
         end loop;
      end loop;
      Text_IO.Put_Line (L_Trim (Integer'Image (Y (1))) &
               Integer'Image (Y (N)));
   end;
end Ary3;

ary3.guile
#!/usr/local/bin/guile \
-e main -s
!#

;;; $Id: ary3.guile,v 1.3 2001/06/29 23:12:36 doug Exp $
;;; http://www.bagley.org/~doug/shootout/

(define (main args)
  (let* ((n (or (and (= (length args) 2) (string->;number (cadr args))) 1))
     (x (make-vector n 0))
     (y (make-vector n 0))
     (last (- n 1)))
    (do ((i 0 (+ i 1)))
    ((= i n))
      (vector-set! x i (+ i 1)))
    (do ((k 0 (+ k 1)))
    ((= k 1000))
      (do ((i last (- i 1)))
      ((<; i 0))
    (vector-set! y i (+ (vector-ref x i) (vector-ref y i)))))
    (print-list (vector-ref y 0) " " (vector-ref y last))))

(define (print-list . items) (for-each display items) (newline))
ary3.ici
// $Id: ary3.ici,v 1.0 2003/01/03 12:16:00 dada Exp $
// http://dada.perl.it/shootout
//
// contributed by Tim Long

n = argv[1] ? int(argv[1]) : 1;

x = build(n);
for (i = 0; i < n; ++i)
    x[i] = i + 1;

y = build(n, "c", 0);

for (k = 0; k < 1000; ++k)
{
    for (i = n - 1; i >= 0; --i)
        y[i] += x[i];
}

printf("%d %d\n", y[0], y[n - 1]);
ary3.icon
# -*- mode: icon -*-
# $Id: ary3.icon,v 1.1 2001/05/31 02:27:48 doug Exp $
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
    # local n, i, k, x, y, last
    n := argv[1] | 1
    x := list(n,0)
    y := list(n,0)
    every i := 1 to n do {
    x[i] := i
    }
    every k := 0 to 999 do {
    every i := n to 1 by -1 do {
        y[i] +:= x[i]
    }
    }
    write(y[1], " ", y[n])
end
ary3.java
// $Id: ary3.java,v 1.2 2001/05/31 20:17:42 doug Exp $
// http://www.bagley.org/~doug/shootout/

// this program is modified from:
//   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
// Timing Trials, or, the Trials of Timing: Experiments with Scripting
// and User-Interface Languages</a> by Brian W. Kernighan and
// Christopher J. Van Wyk.

import java.io.*;
import java.util.*;

public class ary3 {
    public static void main(String args[]) {
    int i, j, k, n = Integer.parseInt(args[0]);
    int x[] = new int[n];
    int y[] = new int[n];

    for (i = 0; i < n; i++)
        x[i] = i + 1;
    for (k = 0; k < 1000; k++ )
        for (j = n-1; j >= 0; j--)
        y[j] += x[j];

    System.out.println(y[0] + " " + y[n-1]);
    }
}
ary3.jscript
// -*- mode: java -*-
// $Id: ary3.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// By David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

var n, i, k;

ARGS = WScript.Arguments

if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1000;
}
var x = Array(n);
var y = Array(n);

for (i = 0; i < n; i++) {
  x[i] = i + 1;
  y[i] = 0; // Need to set all entries in i to zero or the result will be NaN 
}
for (k = 0 ; k < 1000; k++) {
  for (i = n-1; i >= 0; i--) {
    y[i] += x[i];
  }
}
WScript.Echo(y[0], y[n-1]);
ary3.lcc
/* -*- mode: c -*-
 * $Id: ary3.gcc,v 1.1 2001/05/31 02:27:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *
 * this program is modified from:
 *   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
 * Timing Trials, or, the Trials of Timing: Experiments with Scripting
 * and User-Interface Languages</a> by Brian W. Kernighan and
 * Christopher J. Van Wyk.
 *
 * I added free() to deallocate memory.
 */

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, k, *x, *y;
    
    x = (int *) calloc(n, sizeof(int));
    y = (int *) calloc(n, sizeof(int));

    for (i = 0; i < n; i++) {
    x[i] = i + 1;
    }
    for (k=0; k<1000; k++) {
    for (i = n-1; i >= 0; i--) {
        y[i] += x[i];
    }
    }

    fprintf(stdout, "%d %d\n", y[0], y[n-1]);

    free(x);
    free(y);

    return(0);
}
ary3.lua
-- $Id: ary3.lua,v 1.1 2001/05/31 02:27:48 doug Exp $
-- http://www.bagley.org/~doug/shootout/

local n = tonumber((arg and arg[1]) or 1)

local x, y = {}, {}
local last = n - 1

for i=0,last do
  x[i] = i + 1
  y[i] = 0
end
for k=1,1000 do
  for j=last,0,-1 do
    y[j] = y[j] + x[j]
  end
end

write(y[0], " ", y[last], "\n")
ary3.lua5
-- $Id: ary3.lua,v 1.1 2001/05/31 02:27:48 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- contributed by Roberto Ierusalimschy

local n = tonumber((arg and arg[1]) or 1)

local x, y = {}, {}

for i=1,n do
  x[i] = i + 1
  y[i] = 0
end
for k=1,1000 do
  for j=n,1,-1 do
    y[j] = y[j] + x[j]
  end
end

io.write(y[1], " ", y[n], "\n")

ary3.mawk
# $Id: ary3.mawk,v 1.1 2001/05/31 02:27:48 doug Exp $
# http://www.bagley.org/~doug/shootout/

# this program modified from:
#   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
# Timing Trials, or, the Trials of Timing: Experiments with Scripting
# and User-Interface Languages</a> by Brian W. Kernighan and
# Christopher J. Van Wyk.

BEGIN {
    n = (ARGV[1] < 1) ? 1 : ARGV[1];

    for (i = 0; i < n; i++)
    x[i] = i + 1
    for (k = 0; k < 1000; k++) {
    for (j = n-1; j >= 0; j--)
        y[j] += x[j]
    }

    print y[0], y[n-1]
}
ary3.mercury
%% $Id: ary3.mercury,v 1.1 2001/05/31 02:27:48 doug Exp $
%% http://www.bagley.org/~doug/shootout/
%% based on some code from Ralph Becket

:- module mytest.

:- interface.

:- import_module io.



:- pred main(io__state, io__state).
:- mode main(di, uo) is det.



:- implementation.



:- import_module array, int, list, string, require.



main -->
    io__command_line_arguments(ArgV),
    (   { ArgV = [],        N = 1 }
    ;   { ArgV = [Arg],     N = string__det_to_int(Arg) }
    ;   { ArgV = [_,_|_],   error("usage: arrayaccess [N]") }
    ),
    { X = some_naturals(0, array__init(N, 0)) },
    { Y = add_arrays_n(1000, N-1, X, array__init(N, 0)) },
    io__write_int(array__lookup(Y, 0)),
    io__write_string(" "),
    io__write_int(array__lookup(Y, N - 1)),
    io__nl.



:- func some_naturals(int, array(int)) = array(int).
:- mode some_naturals(in, array_di) = array_uo is det.

some_naturals(I, A) =
    ( if I =< array__max(A) then some_naturals(I + 1, array__set(A, I, I + 1))
                            else A ).



:- func add_array(int, array(int), array(int)) = array(int).
:- mode add_array(in, array_ui, array_di) = array_uo is det.

add_array(I, A, B) =
    ( if I < 0
      then B
      else add_array(I - 1, A, array__set(B, I, array__lookup(A, I) + array__lookup(B, I)))
    ).



:- func add_arrays_n(int, int, array(int), array(int)) = array(int).
:- mode add_arrays_n(in, in, array_ui, array_di) = array_uo is det.

add_arrays_n(N, Len, A, B) =
    ( if N > 0
      then add_arrays_n(N - 1, Len, A, add_array(Len, A, B))
      else B
    ).

ary3.mingw32
/* -*- mode: c -*-
 * $Id: ary3.gcc,v 1.1 2001/05/31 02:27:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *
 * this program is modified from:
 *   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
 * Timing Trials, or, the Trials of Timing: Experiments with Scripting
 * and User-Interface Languages</a> by Brian W. Kernighan and
 * Christopher J. Van Wyk.
 *
 * I added free() to deallocate memory.
 */

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, k, *x, *y;
    
    x = (int *) calloc(n, sizeof(int));
    y = (int *) calloc(n, sizeof(int));

    for (i = 0; i < n; i++) {
    x[i] = i + 1;
    }
    for (k=0; k<1000; k++) {
    for (i = n-1; i >= 0; i--) {
        y[i] += x[i];
    }
    }

    fprintf(stdout, "%d %d\n", y[0], y[n-1]);

    free(x);
    free(y);

    return(0);
}
ary3.modula2
(* The Great Win32 Language Shootout http://dada.perl.it/shootout/

   contributed by Isaac Gouy (Modula2 novice)

   To compile: xc =m ary3
   To run:     ary3 7000
*)

MODULE Ary3;
<* m2extensions + *>
<* storage + *>
<* ioverflow - *>
<* noptralias + *>
<* checkdindex - *>
<* checknil - *>

(* Prefer unqualified procedures *)
FROM LanguageShootout IMPORT N;

FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteInt;

TYPE
   Array_Type = ARRAY OF INTEGER;
   Array_Ptr_Type = POINTER TO Array_Type;

VAR
   n, i, m, j: INTEGER;
   x, y:  Array_Ptr_Type;

BEGIN
   n := N();

   NEW(x, n);
   NEW(y, n);

   FOR i := 0 TO INT(HIGH(x^)) DO
      x^[i] := i+1;
      y^[i] := 0;
   END;

   m := HIGH(y^);
   FOR j := 1 TO 1000 DO
      FOR i := 0 TO m DO
         INC(y^[i], x^[i]);
      END;
   END;

   WriteInt(y^[0],1); WriteInt(y^[HIGH(y^)],0); WriteLn;

   DISPOSE(x);
   DISPOSE(y);
END Ary3.
ary3.nice
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a ary3.jar ary3

To run:
   java -jar ary3.jar 7000
*/


// NOTE: the types of n,x,y,i,k,j will be 
//       inferred by the compiler


import ackermann; // reuse toSingleInt

void main(String[] args){
   let n = toSingleInt(args);

   let x = new int[n];
   for(var i=0; i=0; j--) y[j] += x[j];

   print(y[0]); print(" "); println(y[n-1]);
}
ary3.ocaml
(*
 * $Id: ary3.ocaml,v 1.1 2001/05/31 02:27:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * with help from Markus Mottl
 *)

let _ =
  let n =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  let last = n-1
  and x = Array.make n 0
  and y = Array.make n 0 in
  for i = 0 to last do
    x.(i) <- (i + 1)
  done;
  for k = 0 to 999 do
    for i = last downto 0 do
      y.(i) <- (x.(i) + y.(i))
    done
  done;
  Printf.printf "%d %d\n" y.(0) y.(last)
ary3.ocamlb
(*
 * $Id: ary3.ocaml,v 1.1 2001/05/31 02:27:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * with help from Markus Mottl
 *)

let _ =
  let n =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  let last = n-1
  and x = Array.make n 0
  and y = Array.make n 0 in
  for i = 0 to last do
    x.(i) <- (i + 1)
  done;
  for k = 0 to 999 do
    for i = last downto 0 do
      y.(i) <- (x.(i) + y.(i))
    done
  done;
  Printf.printf "%d %d\n" y.(0) y.(last)
ary3.oz
%%% $Id: ary3.oz,v 1.0 2002/04/02 16:17:00 dada Exp $
%%% http://dada.perl.it/shootout/

%%% Code contributed by Andrew McDowell

functor
import
  System
  Application
define
 local Args N A1 A2 in

   {Application.getCmdArgs plain Args}
   if {List.length Args} \= 1 then 
     N = 1
   else
     {String.toInt Args.1 N}
   end

   {NewArray 0 N 0 A1} 
   {NewArray 0 N 0 A2} 

   {For 0 (N - 1) 1 
     proc {$ I} {Put A1 I (I + 1)} end }

   {For 0 999 1 
      proc {$ I} 
        {For (N - 1) 0 ~1 
          proc {$ I} {Put A2 I ({Array.get A2 I} + {Get A1 I})} end}
      end}


   {System.showInfo {Get A2 0}#" "#{Get A2 (N - 1)}}
   {Application.exit 0}
 end
end

ary3.parrot
        set I0, P0[1]
        new P1, .Array
        new P2, .Array

        set P1, I0
        set P2, I0
        dec I0
    
        set I1, 0
        set I2, 1
X_LOOP:    gt I1, I0, X_DONE
        set P1[I1], I2
        inc I1
        inc I2
        branch X_LOOP

X_DONE:
        set I1, 0
Y_LOOP:    gt I1, 999, Y_DONE

        set I2, I0
Z_LOOP:    lt I2, 0, Z_DONE
        set I3, P2[I2]
        set I4, P1[I2]
        add I3, I4
        set P2[I2], I3
        dec I2
        branch Z_LOOP
Z_DONE:
        inc I1
        branch Y_LOOP
Y_DONE:
        set I1, P2[0]
        print I1
        print " "
        set I1, P2[I0]
        print I1
        print "\n"
        end
ary3.perl
#!/usr/local/bin/perl
# $Id: ary3.perl,v 1.1 2001/05/31 02:27:48 doug Exp $
# http://www.bagley.org/~doug/shootout/

# this program is modified from:
#   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
# Timing Trials, or, the Trials of Timing: Experiments with Scripting
# and User-Interface Languages</a> by Brian W. Kernighan and
# Christopher J. Van Wyk.

my $n = @ARGV[0] || 1;
my @X;
my @Y;

my $last = $n - 1;
for my $i (0..$last) {
    $X[$i] = $i + 1;
}
for my $k (0..999) {
    for my $i (reverse 0..$last) {
    $Y[$i] += $X[$i];
    }
}

print "$Y[0] $Y[$last]\n";
ary3.php
<?php
/*
 $Id: ary3.php,v 1.1 2001/05/31 02:27:48 doug Exp $
 http://www.bagley.org/~doug/shootout/
*/
$n = ($argc == 2) ? $argv[1] : 1;
for ($i=0; $i<$n; $i++) {
    $X[$i] = $i + 1;
}
for ($k=0; $k<1000; $k++) {
    for ($i=$n-1; $i>=0; $i--) {
    $Y[$i] += $X[$i];
    }
}
$last = $n-1;
print "$Y[0] $Y[$last]\n";
?>
ary3.pike
#!/usr/local/bin/pike// -*- mode: pike -*-
// $Id: ary3.pike,v 1.1 2001/05/31 02:27:48 doug Exp $
// http://www.bagley.org/~doug/shootout/

void main(int argc, array(string) argv)
{
    int n = (int)argv[-1];
    if (n < 1) n = 1;

    array(int) x = allocate(n);
    array(int) y = allocate(n);

    for (int i; i<n; i++) {
    x[i] = i + 1;
    }
    for (int k; k<1000; k++) {
    for (int i=n-1; i>=0; i--) {
        y[i] += x[i];
    }
    }
    write("%d %d\n", y[0], y[-1]);
}
ary3.pliant
# $Id: ary3.pliant,v 1.0 2002/02/08 12:30:00 dada Exp $
# http://dada.perl.it/shootout/

module "/pliant/language/context.pli"

gvar Int i
gvar Int k
gvar Int last
gvar Array:Int X
gvar Array:Int Y
gvar Str s_n := cast ((pliant_script_args translate Address 1) map CStr) Str
if (s_n parse (gvar Int n))
  last := n - 1  
  X:size = n
  Y:size = n
  for i 0 last
    X += i + 1
    Y += 0
  for k 0 999
    for i last 0 step -1
      Y:i += X:i
  console Y:0 " " Y:last eol
else
  console "usage: ary3.pliant <number>" eol
  
ary3.poplisp
;;; -*- mode: lisp -*-
;;; $Id: ary3.poplisp,v 1.0 2002/05/03 12:16:00 dada Exp $

(let ((n (parse-integer (or (car pop11::poparglist) "1"))))
(declare (fixnum n))
(let ((x (make-array n :element-type 'fixnum))
  (y (make-array n :element-type 'fixnum))
  (last (1- n)))
  (declare (fixnum last))
  (dotimes (i n)
(declare (fixnum i))
(setf (aref x i) (+ i 1)))
  (dotimes (k 1000)
(do ((i last (1- i)))
    ((<; i 0) 'nil)
  (declare (fixnum i))
  (incf (aref y i) (aref x i))))
  (format t "~A ~A~%" (aref y 0) (aref y last))))
ary3.python
#!/usr/local/bin/python
# $Id: ary3.python,v 1.1 2001/05/31 02:27:48 doug Exp $
# http://www.bagley.org/~doug/shootout/
# with help from Brad Knotwell

import sys

def main():
    n = int(sys.argv[1])
    x = n * [0]       
    y = n * [0]
    for i in xrange(0,n):
        x[i] = i + 1
    for k in xrange(0,1000):
        for i in xrange(n-1,-1,-1):
            y[i] += x[i]
    print y[0], y[-1]

main()
ary3.rexx
parse arg n
If n < 1 Then Do
    n = 1
End

last = n - 1

Y.0 = 0
Do i = 0 To last
    X.i = i + 1
    Y.i = 0
End

Do k = 0 To 999
    Do i = last To 0 By -1
        Y.i = Y.i + X.i
    End
End

say Y.0" "Y.last
ary3.ruby
#!/usr/local/bin/ruby
# -*- mode: ruby -*-
# $Id: ary3.ruby,v 1.1 2001/05/31 02:27:48 doug Exp $
# http://www.bagley.org/~doug/shootout/

n = Integer(ARGV.shift || 1)

i = 0
x = Array.new(n)
y = Array.new(n)
last = n-1

for i in 0 .. last
    x[i] = i + 1
    y[i] = 0
end
for k in 0 .. 999
    last.step(0,-1) do |i|
    y[i] += x[i]
    end
end

puts "#{y[0]} #{y[last]}"
ary3.se
-- -*- mode: eiffel -*-
-- $Id: ary3.se,v 1.1 2001/05/31 02:27:48 doug Exp $
-- http://www.bagley.org/~doug/shootout/

-- Friedrich Dominicus points out that it is about twice as fast
-- when we substitute NATIVE_ARRAY for ARRAY.  I've commented out
-- my original code below and now use NATIVE_ARRAY.

class ARY3

creation make

feature

   make is

      local
         x: NATIVE_ARRAY[INTEGER];
         y: NATIVE_ARRAY[INTEGER];
--     x: ARRAY[INTEGER];
--     y: ARRAY[INTEGER];
     i,k,n: INTEGER;
      do
     if argument_count = 1 then
        n := argument(1).to_integer
     else
        n := 1
     end

         x := x.calloc(n);
         y := y.calloc(n);
--     !!x.make(0,n)
--     !!y.make(0,n)

     from
        i := 0
     until
        i = n
     loop
        x.put(i + 1, i)
        i := i + 1
     end
     from
        k := 0
     until
        k = 1000
     loop
        from
           i := n-1
        until
           i < 0
        loop
           y.put(x.item(i) + y.item(i),i)
           i := i - 1
        end
        k := k + 1
     end

         std_output.put_integer(y.item(0))
         std_output.put_character(' ')
         std_output.put_integer(y.item(n-1))
         std_output.put_character('%N')
      end
end
ary3.slang
% $Id: ary3.slang,v 1.0 2003/01/03 14:06:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis


define main_slow()
{
   variable n = integer (__argv[1]);
   variable x = Int_Type[n];
   variable y = Int_Type[n];
   _for (0,n-1,1)
     {
    variable i = ();
        x[i] = i + 1;
     }
   loop (1000)
     {
    i = [n-1:0:-1];
    y[i] += x[i];
     }
   
   vmessage ("%S %S", y[0], y[-1]);
}

% define main_fast ()
% {
%    variable n = integer (__argv[1]);
%    variable x = [1:n];
%    variable y = Int_Type[n];
% 
%    loop (1000)
%      y += x;
% 
%    vmessage ("%S %S", y[0], y[-1]);
% }

main_slow();
ary3.smlnj
(* -*- mode: sml -*-
 * $Id: ary3.smlnj,v 1.2 2001/07/09 00:25:27 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *)

structure Test : sig
    val main : (string * string list) -> OS.Process.status
end = struct

fun index i = i;

fun ary n =
    let
    val x = Array.array(n, 0)
    val y = Array.array(n, 0)
    fun xinit i =
        if i = n then ()
        else (Array.update(x, i, i + 1) ; xinit (i + 1))
    fun xtoy i =
        if i < 0 then ()
        else (Array.update(y, i, Array.sub(x, i) + Array.sub(y, i)) ; xtoy (i - 1))
        fun aryloop i =
        if i < 0 then ()
        else (xtoy(n-1); aryloop (i-1))
    in
    xinit 0;
    aryloop 999;
    print (Int.toString (Array.sub(y, 0)));
    print " ";
    print (Int.toString (Array.sub(y, (n-1))));
    print "\n"
    end;

fun atoi s = case Int.fromString s of SOME num => num | NONE => 0;

fun main(name, args) = 
    let
    val arg = hd(args @ ["1"])
    val num = atoi arg
    in
    ary num;
    OS.Process.success
    end
end

val _ = SMLofNJ.exportFn("ary3", Test.main);
ary3.tcl
#!/usr/local/bin/tclsh
# $Id: ary3.tcl,v 1.1 2001/05/31 02:27:48 doug Exp $
# http://www.bagley.org/~doug/shootout/

# this program is modified from:
#   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
# Timing Trials, or, the Trials of Timing: Experiments with Scripting
# and User-Interface Languages</a> by Brian W. Kernighan and
# Christopher J. Van Wyk.

proc main {} {
    global argv
    set n [lindex $argv 0]
    set last [expr {$n - 1}]
    for {set i 0} {$i < $n} {incr i} {
    set x($i) [expr {$i + 1}]
    set y($i) 0
    }
    for {set k 0} {$k < 1000} {incr k} {
    for {set j $last} {$j >= 0} {incr j -1} {
        set y($j) [expr {$x($j) + $y($j)}]
    }
    }
    puts "$y(0) $y($last)"
}

main
ary3.vbscript

n = WScript.Arguments(0)
Redim X(n), Y(n)

If n < 1 Then n = 1
last = n - 1
For i = 0 To last
    X(i) = i + 1
Next
For K = 0 To 999
    For I = last To 0 Step -1
        Y(i) = Y(i) + X(i)
    Next
Next

WScript.Echo Y(0) & " " & Y(last)
ary3.vc
/* -*- mode: c -*-
 * $Id: ary3.gcc,v 1.1 2001/05/31 02:27:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *
 * this program is modified from:
 *   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
 * Timing Trials, or, the Trials of Timing: Experiments with Scripting
 * and User-Interface Languages</a> by Brian W. Kernighan and
 * Christopher J. Van Wyk.
 *
 * I added free() to deallocate memory.
 */

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, k, *x, *y;
    
    x = (int *) calloc(n, sizeof(int));
    y = (int *) calloc(n, sizeof(int));

    for (i = 0; i < n; i++) {
    x[i] = i + 1;
    }
    for (k=0; k<1000; k++) {
    for (i = n-1; i >= 0; i--) {
        y[i] += x[i];
    }
    }

    fprintf(stdout, "%d %d\n", y[0], y[n-1]);

    free(x);
    free(y);

    return(0);
}
ary3.vc++
// -*- mode: c++ -*-
// $Id: ary3.g++,v 1.2 2001/06/20 03:20:02 doug Exp $
// http://www.bagley.org/~doug/shootout/

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
    int i, k, n = ((argc == 2) ? atoi(argv[1]) : 1);
    typedef vector<int> ARY;
    ARY x(n);
    ARY y(n);

    for (i=0; i<n; i++) {
    x[i] = i + 1;
    }
    for (k=0; k<1000; k++) {
    for (int i = n - 1; i >= 0; --i) {
        y[i] += x[i];
    }
    }

    cout << y[0] << " " << y.back() << endl;
}
ary3.vpascal
Program ary3;

uses SysUtils, Classes;

var
    n, i, k, last : integer;
    X, Y : TList;
begin
    if ParamCount = 0 then
        n := 1
    else
        n := StrToInt(ParamStr(1));
        
    if n < 1 then n := 1;
    
    last := n - 1;
    X := TList.Create;
    X.Capacity := n;
    For i := 0 To last do
        X.Add( Pointer(i+1) );
    
    Y := TList.Create;
    Y.Capacity := n;
    For i := 0 To last do
        Y.Add( Pointer(0) );
    
    
    For k := 0 To 999 do
    begin
        For i := last downto 0 do
        begin
            Y.Items[i] := Pointer(Integer(Y.Items[i]) + Integer(X.Items[i]));
        end;
    end;
    Writeln (IntToStr(Integer(Y.Items[0])), ' ', IntToStr(Integer(Y.Items[last])));
end.