String Concatenation Back to the Win32 Shootout
Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]  
All Source For String Concatenation
strcat.awka
# $Id: strcat.gawk,v 1.3 2001/05/20 06:13:00 doug Exp $
# http://www.bagley.org/~doug/shootout/

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

    str = ""
    for (i = 0; i < n; i++)
    str = str "hello\n"

    print length(str)
}
strcat.bcc
/* -*- mode: c -*-
 * $Id: strcat.gcc,v 1.7 2001/05/02 05:55:44 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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

#define STUFF "hello\n"

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, buflen = 32;
    char *strbuf = calloc(sizeof(char), buflen);
    char *strend = strbuf;
    int stufflen = strlen(STUFF);

    if (!strbuf) { perror("calloc strbuf"); exit(1); }
    for (i=0; i<n; i++) {
    if (((strbuf+buflen)-strend) < (stufflen+1)) {
        buflen = 2*buflen;
        strbuf = realloc(strbuf, buflen);
        if (!strbuf) { perror("realloc strbuf"); exit(1); }
        strend = strbuf + strlen(strbuf);
    }
    
    strcat(strend, STUFF);
    strend += stufflen;
    }
    fprintf(stdout, "%d\n", strlen(strbuf));
    free(strbuf);

    return(0);
}
strcat.bigforth
\ -*- mode: forth -*-
\ $Id: strcat.bigforth,v 1.2 2001/06/24 17:08:56 doug Exp $
\ http://www.bagley.org/~doug/shootout/

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

variable hsiz    32                       hsiz !  \ buffer can hold this much
variable hbuf    hsiz @ allocate throw    hbuf !  \ start of buffer
variable hoff    0                        hoff !  \ current offset in buffer

: STUFF s" hello." ;

: strcat 
    dup                              \ dup strlen on stack
    hsiz @ hoff @ - >                \ if strlen > remaining space
    if                               \ reallocate buffer
    hsiz @ 2* hsiz !             \ double size
    hbuf @ hsiz @ resize throw   \ reallocate buffer
    hbuf !                       \ store (possibly new) buffer start
    then
    swap over                        \ stack: strlen straddr strlen
    hbuf @ hoff @ +
    swap cmove>                      \ append from straddr to hbuf+hoff
    hoff @ + hoff !                  \ update hoff
    ;

: main 
    NUM 0
    do
    STUFF strcat
    loop
    \ as a final result push the resultant string on the stack as if we
    \ were going to use it for something.
    hbuf @ hoff @
    \ and print out the length
    1 u.r cr drop ;

main

bye \ th-th-that's all folks!
strcat.csharp
// $Id: strcat.csharp,v 1.1 2002/10/16 16:52:00 dada Exp $
// http://dada.perl.it/shootout/
//
// code contributed by Erik Saltwell  

using System;

class App {

    public static int Main(String[] args) {
        int N;
        N = int.Parse(args[0]);
        if(N < 1) N = 1;

        System.Text.StringBuilder sb = new System.Text.StringBuilder(32);

        for (int i=0; i<N; i++) {
            sb.Append("hello\n");
        }

        Console.WriteLine(sb.Length);
        return(0);
    }
}
strcat.cygperl
#!/usr/local/bin/perl 
# $Id: strcat.perl,v 1.4 2001/04/29 06:13:05 doug Exp $
# http://www.bagley.org/~doug/shootout/

use strict;

my $NUM = $ARGV[0];
$NUM = 1 if ($NUM < 1);

my $str = "";
$str .= "hello\n" foreach (1..$NUM);
print length($str),"\n";

strcat.delphi
program strcat;


var NUM, code, i: integer;
    str : string;
begin
  NUM :=1;
  if ParamCount=1 then
    Val(ParamStr(1),NUM,code);

  str := '';
  For i := 1 To NUM Do
     str := str + 'hello'#10;
  WriteLn(Length(str));
end.

strcat.elastic
// $Id: strcat.elastic,v 1.0 2002/05/09 17:22:00 dada Exp $
package strcat;

import basic;
import sys;
import array;
import string;

private n = 1;
private str = "";
if(array.length(sys.args) > 0) {
    n = basic.int(sys.args[0]);
} else {
    n = 1;
}
while(n--) {
    str = str + "hello\n";
}
basic.print(string.length(str));
strcat.erlang
%%% -*- mode: erlang -*-
%%% $Id: strcat.erlang,v 1.1 2001/05/15 07:37:04 doug Exp $
%%% http://www.bagley.org/~doug/shootout/

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

main([Arg]) ->
    Num = list_to_integer(atom_to_list(Arg)),
    io:fwrite("~w\n", [length(string:copies("hello\n", Num))]),
    halt(0).
strcat.fpascal
program strcat;

uses SysUtils;
var 
    NUM, i : longint;
    str : string;

begin
    if ParamCount = 0 then NUM := 1
    else NUM := StrToInt(ParamStr(1));
    if NUM < 1 then NUM := 1;

    str := '';
    For i := 1 To NUM Do
        str := str + 'hello'#13;
    WriteLn( Longint(Length(str)) );
    WriteLn( str );    
end.
strcat.gawk
# $Id: strcat.gawk,v 1.3 2001/05/20 06:13:00 doug Exp $
# http://www.bagley.org/~doug/shootout/

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

    str = ""
    for (i = 0; i < n; i++)
    str = str "hello\n"

    print length(str)
}
strcat.gcc
/* -*- mode: c -*-
 * $Id: strcat.gcc,v 1.7 2001/05/02 05:55:44 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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

#define STUFF "hello\n"

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, buflen = 32;
    char *strbuf = calloc(sizeof(char), buflen);
    char *strend = strbuf;
    int stufflen = strlen(STUFF);

    if (!strbuf) { perror("calloc strbuf"); exit(1); }
    for (i=0; i<n; i++) {
    if (((strbuf+buflen)-strend) < (stufflen+1)) {
        buflen = 2*buflen;
        strbuf = realloc(strbuf, buflen);
        if (!strbuf) { perror("realloc strbuf"); exit(1); }
        strend = strbuf + strlen(strbuf);
    }
    
    strcat(strend, STUFF);
    strend += stufflen;
    }
    fprintf(stdout, "%d\n", strlen(strbuf));
    free(strbuf);

    return(0);
}
strcat.gforth
\ -*- mode: forth -*-
\ $Id: strcat.gforth,v 1.4 2001/06/24 17:08:56 doug Exp $
\ http://www.bagley.org/~doug/shootout/

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

variable hsiz    32                       hsiz !  \ buffer can hold this much
variable hbuf    hsiz @ allocate throw    hbuf !  \ start of buffer
variable hoff    0                        hoff !  \ current offset in buffer

: STUFF s" hello." ;

: strcat 
    dup                              \ dup strlen on stack
    hsiz @ hoff @ - >                \ if strlen > remaining space
    if                               \ reallocate buffer
    hsiz @ 2* hsiz !             \ double size
    hbuf @ hsiz @ resize throw   \ reallocate buffer
    hbuf !                       \ store (possibly new) buffer start
    then
    swap over                        \ stack: strlen straddr strlen
    hbuf @ hoff @ +
    swap cmove>                      \ append from straddr to hbuf+hoff
    hoff @ + hoff !                  \ update hoff
    ;

: main 
    NUM 0
    do
    STUFF strcat
    loop
    \ as a final result push the resultant string on the stack as if we
    \ were going to use it for something.
    hbuf @ hoff @
    \ and print out the length
    1 u.r cr drop ;

main

bye \ th-th-that's all folks!
strcat.ghc
-- $Id: strcat.ghc,v 1.3 2001/06/16 01:24:09 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Josef Svenningsson
-- shortened by Bryn Keller

import System(getArgs, getProgName)

lengthNHellos n = length (concat (replicate n "hello\n"))

main = do
       arg <- getArgs
       case arg of
         [number] -> putStrLn $ show $ lengthNHellos (read number)
         _        -> do name <- getProgName; fail ("Usage: " ++ name ++ "number")

strcat.gnat
-- $Id: strcat.gnat,v 1.0 2003/11/18 12:15:00 dada Exp $
-- http://dada.perl.it/shootout
-- contributed by James S. Rogers

with Ada.Command_Line; use Ada.Command_Line;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_text_Io; use Ada.Integer_Text_Io;

procedure Strcat is
   N: Integer;
   Hello : String := "hello" & Ascii.Lf;
   Buffer : Unbounded_String := Null_Unbounded_String;
begin
   N := Integer'Value(Argument(1));
   for Num in 1..N loop
      Append(Source => Buffer, New_Item => Hello);
   end loop;
   Put(Length(Buffer));
   New_Line;
end Strcat;
strcat.guile
#!/usr/local/bin/guile-oops \
-e main -s
!#

;;; $Id: strcat.guile,v 1.4 2001/06/29 23:12:37 doug Exp $
;;; http://www.bagley.org/~doug/shootout/
;;; from Benedikt Rosenau

(use-modules (oop goops))

(define-class <;buffer> ()
  (siz #:getter buffer-size #:init-value 64)
  (len #:getter buffer-length #:init-value 0)
  (field #:init-value (make-string 64)))

(define-method (buffer->;string (b <buffer>))
  (substring (slot-ref b 'field) 0 (buffer-length b)))

(define-method (buffer-append! (b <;buffer>) (s <string>))
  (let* ((length-b (buffer-length b))
         (size-b (buffer-size b))
         (length-s (string-length s))
         (new-length (+ length-b length-s)))
    (if (>; new-length size-b)
      (let* ((new-size (+ size-b (max length-b length-s)))
             (new-field (make-string new-size)))
        (substring-move-left! (slot-ref b 'field) 0 length-b new-field 0)
        (slot-set! b 'field new-field)
        (slot-set! b 'siz new-size)))
    (substring-move-left! s 0 length-s (slot-ref b 'field) length-b)
    (slot-set! b 'len new-length)
     b))


(define-method (main (args <;list>))
  (let ((n (or (and (= (length args) 2) (string->;number (cadr args))) 1))
    (buf (make <;buffer>)))
    (do ((i 0 (+ i 1))) ((= i n))
        (buffer-append! buf "hello\n"))
    (display (buffer-length buf))
    (newline)))
strcat.ici
// $Id: strcat.ici,v 1.0 2003/01/03 11:29:00 dada Exp $
// http://dada.perl.it/shootout
//
// contributed by Tim Long

n := argv[1] ? int(argv[1]) : 1;
s := "";
for (i = 0; i < n; ++i)
    s += "hello\n";
printf("%d\n", nels(s));
strcat.icon
# -*- mode: icon -*-
# $Id: strcat.icon,v 1.1 2000/12/17 23:46:59 doug Exp $
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
    n := argv[1] | 1;
    str := "";
    every i := 1 to n do str ||:= "hello\n";
    write(*str);
end
strcat.java
// $Id: strcat.java,v 1.6 2001/06/28 15:36:52 doug Exp $
// http://www.bagley.org/~doug/shootout/
// Pelle Nilsson suggested we also illustrate StringBuffer
// since it is the preferred method for concatenating 
// strings in Java

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

public class strcat {
    public static void main(String args[]) throws IOException {
    int n = Integer.parseInt(args[0]);
    String hello = "hello\n";
    StringBuffer stringBuffer = new StringBuffer(32);

        for (int i=0; i<n; i++) {
            stringBuffer.append(hello);
    }

        System.out.println(stringBuffer.toString().length());
    }
}
strcat.jscript
// -*- mode: java -*-
// $Id: strcat.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// From: David Hedbor

var n;

ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1;
}
var str = new String("");
while(n--) {  str += "hello\n"; }
WScript.Echo(str.length);
strcat.lcc
/* -*- mode: c -*-
 * $Id: strcat.gcc,v 1.7 2001/05/02 05:55:44 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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

#define STUFF "hello\n"

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, buflen = 32;
    char *strbuf = calloc(sizeof(char), buflen);
    char *strend = strbuf;
    int stufflen = strlen(STUFF);

    if (!strbuf) { perror("calloc strbuf"); exit(1); }
    for (i=0; i<n; i++) {
    if (((strbuf+buflen)-strend) < (stufflen+1)) {
        buflen = 2*buflen;
        strbuf = realloc(strbuf, buflen);
        if (!strbuf) { perror("realloc strbuf"); exit(1); }
        strend = strbuf + strlen(strbuf);
    }
    
    strcat(strend, STUFF);
    strend += stufflen;
    }
    fprintf(stdout, "%d\n", strlen(strbuf));
    free(strbuf);

    return(0);
}
strcat.lua
-- $Id: strcat.lua,v 1.2 2001/01/31 03:38:54 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Roberto Ierusalimschy

-- this version uses a custom string buffer

------------------------------------------------------------------
-- Buffer library
------------------------------------------------------------------

Buffer = {n=0}

function Buffer:new ()
  local new = {}
  for k,v in self do new[k] = v end
  return new
end

function Buffer:add (s)
  tinsert(self, s)
  local i = self.n
  for i=self.n-1, 1, -1 do
    if strlen(self[i]) > strlen(self[i+1]) then break end
    local top = tremove(self)
    self[i] = self[i]..top
  end
end

function Buffer:close ()
  for i=self.n-1, 1, -1 do
    local top = tremove(self)
    self[i] = self[i]..top
  end
  return self[1]
end


------------------------------------------------------------------
-- Test
------------------------------------------------------------------

local n = tonumber((arg and arg[1]) or 1)
local buff = Buffer:new()
for i=1,n do
  buff:add("hello\n")
end
write(strlen(buff:close()), "\n")
strcat.lua5
-- $Id: strcat.lua,v 1.2 2001/01/31 03:38:54 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- contributed by Roberto Ierusalimschy

-- this version uses a custom string buffer

------------------------------------------------------------------
-- Buffer library
------------------------------------------------------------------

Buffer = {""}

function Buffer:new ()
  local new = {}
  self.__index = self
  setmetatable(new, self)
  return new
end

function Buffer:add (s)
  table.insert(self, s)    -- push 's' into the the stack
  for i=table.getn(self)-1, 1, -1 do
    if string.len(self[i]) > string.len(self[i+1]) then
      break
    end
    self[i] = self[i] .. table.remove(self)
  end
end

function Buffer:close ()
  self[1] = table.concat(self)
  table.setn(self, 1)   -- now there is only one element
  return self[1]
end


------------------------------------------------------------------
-- Test
------------------------------------------------------------------

local n = tonumber((arg and arg[1]) or 1)
local buff = Buffer:new()
for i=1,n do
  buff:add("hello\n")
end
io.write(string.len(buff:close()), "\n")

strcat.mawk
# $Id: strcat.mawk,v 1.2 2001/05/20 06:13:00 doug Exp $
# http://www.bagley.org/~doug/shootout/

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

    str = ""
    for (i = 0; i < n; i++)
    str = str "hello\n"

    print length(str)
}
strcat.mercury
% ---------------------------------------------------------------------------- %
% stringconcat.m
% Ralph Becket <rbeck@microsoft.com>
% Tue Jan  9 15:56:12 GMT 2001
% vim: ts=4 sw=4 et tw=0 wm=0 ff=unix
%
% NOTE: The C version simply appends to the end of a preallocated
% buffer, doubling the buffer size when necessary.  Not what I would strictly
% call string concatenation.
%
% ---------------------------------------------------------------------------- %

:- module mytest.

:- interface.

:- import_module io.



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



:- implementation.


:- import_module string, int, list, require.



main -->
    io__command_line_arguments(ArgV),
    (   { ArgV = [],        N = 1 }
    ;   { ArgV = [Arg],     N = string__det_to_int(Arg) }
    ;   { ArgV = [_,_|_],   error("usage: nestedloops [N]") }
    ),
    io__write_int(string__length(hellos(N, ""))),
    io__nl.



:- func hellos(int, string) = string.

hellos(I, S) = ( if I > 0 then hellos(I - 1, S ++ "hello\n") else S ).




strcat.mingw32
/* -*- mode: c -*-
 * $Id: strcat.gcc,v 1.7 2001/05/02 05:55:44 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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

#define STUFF "hello\n"

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, buflen = 32;
    char *strbuf = calloc(sizeof(char), buflen);
    char *strend = strbuf;
    int stufflen = strlen(STUFF);

    if (!strbuf) { perror("calloc strbuf"); exit(1); }
    for (i=0; i<n; i++) {
    if (((strbuf+buflen)-strend) < (stufflen+1)) {
        buflen = 2*buflen;
        strbuf = realloc(strbuf, buflen);
        if (!strbuf) { perror("realloc strbuf"); exit(1); }
        strend = strbuf + strlen(strbuf);
    }
    
    strcat(strend, STUFF);
    strend += stufflen;
    }
    fprintf(stdout, "%d\n", strlen(strbuf));
    free(strbuf);

    return(0);
}
strcat.nice
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

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

To run:
   java -jar strcat.jar 40000
*/


import ackermann; // reuse toSingleInt


void main(String[] args){
   var n = toSingleInt(args);
   String s = "hello\n";
   StringBuffer buffer = new StringBuffer(32);

   while (n-- > 0) buffer.append(s);

   println(buffer.length()); 
}
strcat.ocaml
(*
 * $Id: strcat.ocaml,v 1.6 2001/05/02 05:55:22 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * from: Benedikt Rosenau
 *)

let _ =
  let n =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  let buf = Buffer.create 0 in
  for i = 1 to n do
    Buffer.add_string buf "hello\n"
  done;
  Printf.printf "%d\n" (Buffer.length buf);
strcat.ocamlb
(*
 * $Id: strcat.ocaml,v 1.6 2001/05/02 05:55:22 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * from: Benedikt Rosenau
 *)

let _ =
  let n =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  let buf = Buffer.create 0 in
  for i = 1 to n do
    Buffer.add_string buf "hello\n"
  done;
  Printf.printf "%d\n" (Buffer.length buf);
strcat.oz
%%% $Id: strcat.oz,v 1.0 2002/11/05 12:18:00 dada Exp $
%%% http://dada.perl.it/shootout/
%%%
%%% contributed by Isaac Gouy

%%  Usage: start from command line with
%%     ozc -x strcat.oz -o strcat.oz.exe
%%     strcat.oz.exe 40000

functor
import System Application

define

fun {RepeatAppend N S}
   if N == 0 then S else
      {RepeatAppend N-1 {Append "hello\n" S}} end
end

in
   local Args N in 
      [Args] = {Application.getArgs plain}
      N = {String.toInt Args}

      {System.showInfo {Length {RepeatAppend N nil}} }
   end
   {Application.exit 0}
end

strcat.parrot
# $Id: strcat.parrot,v 1.0 2002/08/20 17:29:00 dada Exp $
# http://dada.perl.it/shootout/

    set I0, P0[1]

    set S0, ""
    
LOOP:
    concat S0, "hello\n"
    dec I0
    if I0, LOOP

    length I0, S0
    print I0
    print "\n"
    end
strcat.perl
#!/usr/local/bin/perl 
# $Id: strcat.perl,v 1.4 2001/04/29 06:13:05 doug Exp $
# http://www.bagley.org/~doug/shootout/

use strict;

my $NUM = $ARGV[0];
$NUM = 1 if ($NUM < 1);

my $str = "";
$str .= "hello\n" foreach (1..$NUM);
print length($str),"\n";

strcat.php
#!/usr/local/bin/php -f<?php
/*
 $Id: strcat.php,v 1.1 2001/05/06 06:00:46 doug Exp $
 http://www.bagley.org/~doug/shootout/
*/
$n = ($argc == 2) ? $argv[1] : 1;
$str = "";
while ($n-- > 0) {
    $str .= "hello\n";
}
$len = strlen($str);
print "$len\n";
?>
strcat.pike
#!/usr/local/bin/pike// -*- mode: pike -*-
// $Id: strcat.pike,v 1.2 2001/05/16 13:32:03 doug Exp $
// http://www.bagley.org/~doug/shootout/
// from Per Hedbor

void main(int argc, array(string) argv) {
    int n = (int)argv[-1];
    if (n < 1) n = 1;
      
    String.Buffer str = String.Buffer();
    function f = str->add;
    for (int i=0; i<n; i++) {
    f("hello\n");
    }
    write("%d\n", strlen(str->get()));
}
strcat.pliant
# $Id: strcat.pliant,v 1.0 2002/02/01 10:58:00 dada Exp $
# http://dada.perl.it/shootout/

module "/pliant/language/context.pli"

gvar Str string
gvar Int i
gvar Str s_n := cast ((pliant_script_args translate Address 1) map CStr) Str
if (s_n parse (gvar Int n))
  for (i) 1 n
    string := string + "hello[lf]"
  console string:len eol
else
  console "usage: strcat.pli <number>" eol

strcat.poplisp
;;; -*- mode: lisp -*-
;;; $Id: strcat.cmucl,v 1.0 2002/05/03 12:06:00 dada Exp $

(defconstant *string* "hello
")
              
(defun string-concat1 (n)
  (declare (fixnum n))
  (let ((str "")
        (used-len 0)
        (string-leng 0)
        (i (1+ n)))
    (declare (fixnum i used-len string-leng))
    (declare (simple-base-string str))
    (declare (optimize (speed 3) (debug 0) (safety 0)))
    (dotimes (i (1- i) (replace (make-string used-len) str))
      (let ((required-length (+ used-len (length *string*))))
        (if (eq string-leng 0)
            (setq str (make-string required-length)
                  string-leng required-length)
          (if (>; required-length string-leng)
              (let ((new-len (+ string-leng string-leng)))
                (let ((new-str (make-string new-len)))
                  (replace new-str str :end2 used-len)
                  (setq str new-str string-leng new-len)))))
        (replace str *string* :start1 used-len)
        (setq used-len required-length)))))

(let ((n (parse-integer (or (car pop11::poparglist) "1"))))
(format t "~A~%" (length (string-concat1 n))))
strcat.python
#!/usr/local/bin/python
# $Id: strcat.python,v 1.5 2001/06/12 02:40:03 doug Exp $
# http://www.bagley.org/~doug/shootout/
# from Brad Knotwell

import sys,cStringIO

def main():
    n = int(sys.argv[1])
    str = cStringIO.StringIO()
    for i in xrange(0,n):
        str.write('hello\n')

    print str.tell()

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

str = ""
Do I=1 to N
    str = str || "hello" || D2C(13)
End
say length(str)
strcat.ruby
#!/usr/local/bin/ruby
# -*- mode: ruby -*-
# $Id: strcat.ruby,v 1.2 2000/12/23 15:52:44 doug Exp $
# http://www.bagley.org/~doug/shootout/

# Benedikt Rosenau suggested using
#   str << "hello\n"
# which is orders of magnitude faster than:
#   str += "hello\n"

n = Integer(ARGV.shift || 1)

str = ''
for i in 1 .. n
    str << "hello\n"
end
puts str.length
strcat.se
-- -*- mode: eiffel -*-
-- $Id: strcat.se,v 1.3 2001/04/29 06:13:05 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from: Friedrich Dominicus

class STRCAT

creation
    make

feature

    concat_string : STRING is "hello%N";

    make is
        local
            i,n : INTEGER;
            str: STRING;
        do
            if argument_count = 1 then
                n := argument(1).to_integer
            else
                n := 1
            end

            from i := 1
                !!str.make(100);
            until i > n
            loop
                str.append(concat_string);
                i := i + 1;
            end;
            io.put_integer(str.count);
            io.put_character ('%N');
        end;


end
strcat.slang
% $Id: strcat.slang,v 1.0 2003/01/03 13:36:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis

define main()
{
   variable n = integer (__argv[1]);
   variable str = typecast ("", BString_Type);
   variable hello = typecast ("hello\n", BString_Type);
   loop (n)
     str += hello;
   vmessage ("%S", strlen (str));
}


main();
strcat.smlnj
(* -*- mode: sml -*-
 * $Id: strcat.smlnj,v 1.6 2001/07/10 04:01:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * from Stephen Weeks
 * Modified by Daniel Wang
 *)

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

fun for (start, stop, f) =
   let
      fun loop i =
     if i > stop
        then ()
     else (f i; loop (i + 1))
   in
      loop start
   end

fun atoi s = case Int.fromString s of SOME num => num | NONE => 0
fun printl [] = print "\n" | printl(h::t) = ( print h ; printl t )

val stuff = "hello\n"

structure Buffer:
   sig
      type 'a t

      val add: 'a t * 'a array -> unit
      val length: 'a t -> int
      val new: 'a -> 'a t
   end =
   struct
      datatype 'a t = T of {dummy: 'a,
                length: int ref,
                elts: 'a array ref}

      fun add (T {dummy, elts, length}, a) =
     let
        val l = !length
        val e = !elts
        val en = Array.length e
        val an = Array.length a
        val e =
           if l + an >= en then    
         let val e' = Array.array(2 * en,dummy)
           val _ = Array.copy {src = e, si = 0,len = SOME en,
                       dst = e',di = 0}
           val _ = elts := e'
         in e'
         end
           else e
        val _ =
           Array.copy {src = a, si = 0, len = NONE,
               dst = e, di = l}
        val _ = length := l + an
     in ()
     end

      fun new (dummy: 'a) = T {dummy = dummy,
                   length = ref 0,
                   elts = ref (Array.array (32, dummy))}

      fun length (T {length, ...}) = !length
   end

fun main (name, args) =
   let
       val stuff =
       Array.tabulate (String.size stuff, fn i => String.sub (stuff, i))
       val n = atoi (hd (args @ ["1"]))
       val b = Buffer.new #"\000"
       val _ = for (1, n, fn _ => Buffer.add (b, stuff))
       val _ = printl [Int.toString (Buffer.length b)]
   in
       OS.Process.success
   end
end

val _ = SMLofNJ.exportFn("strcat", Test.main);
strcat.tcl
#!/usr/local/bin/tclsh
# $Id: strcat.tcl,v 1.4 2001/02/22 01:31:21 doug Exp $
# http://www.bagley.org/~doug/shootout/
# from: Kristoffer Lawson

proc main {n} {
    incr n
    while {[incr n -1]} {
        append str "hello\n"
    }
    puts [string length $str]
}

main [lindex $argv 0]
strcat.vbscript
NUM = WScript.Arguments(0)
If NUM < 1 Then NUM = 1
For A = 1 To NUM
    str = str & "hello" & vbCr
Next
WScript.Echo Len(str)
strcat.vc
/* -*- mode: c -*-
 * $Id: strcat.gcc,v 1.7 2001/05/02 05:55:44 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

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

#define STUFF "hello\n"

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);
    int i, buflen = 32;
    char *strbuf = calloc(sizeof(char), buflen);
    char *strend = strbuf;
    int stufflen = strlen(STUFF);

    if (!strbuf) { perror("calloc strbuf"); exit(1); }
    for (i=0; i<n; i++) {
    if (((strbuf+buflen)-strend) < (stufflen+1)) {
        buflen = 2*buflen;
        strbuf = realloc(strbuf, buflen);
        if (!strbuf) { perror("realloc strbuf"); exit(1); }
        strend = strbuf + strlen(strbuf);
    }
    
    strcat(strend, STUFF);
    strend += stufflen;
    }
    fprintf(stdout, "%d\n", strlen(strbuf));
    free(strbuf);

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

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
    int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
    string str;

    for (i=0; i<n; i++) {
    str += "hello\n";
    }
    cout << str.length() << endl;
}
strcat.vpascal
program strcat;

uses SysUtils;
var 
    NUM, i : integer;
    str : string;

begin
    if ParamCount = 0 then NUM := 1
    else NUM := StrToInt(ParamStr(1));
    if NUM < 1 then NUM := 1;

    str := '';
    For i := 1 To NUM Do
        str := str + 'hello'#13;
    WriteLn( Length(str) );
end.