[net.sources] 'Echo' in three languages, for compiler comparison

arnold@gt-nimbus.UUCP (Arnold Robbins) (04/18/85)

Here are some programs that make for an interesting, although not very useful,
comparison between generated code size for different languages and compilers.

Please no flames; I am not trying to offend anyone with this.

What I have is three versions of "echo", in C, Pascal, and Ada.  The
differences in size are:

-rwxr-xr-x  1 arnold      59392 Apr 18 10:59 echo_in_a*		# ada
-rwxr-xr-x  1 arnold       8192 Apr 18 11:01 echo_in_c*		# c
-rwxr-xr-x  1 arnold      14336 Apr 18 11:30 echo_in_p*		# pascal

They were compiled with the Verdix VADS compiler, the 4.2BSD cc, and the
4.2 BSD pc compiler respectively.  A vice president of Verdix told me that
they will be working on arranging to only load what is needed, so that small
ADA programs will not need such large binaries.  Also, we are using a
Beta Test version of their Ada compiler.
-----------------------------------
/* echo : echo out command line arguments */

#include <stdio.h>

#define TRUE	1
#define FALSE	0

main (argc, argv)
register int argc;
register char **argv;
{
	register int i;			/* loop control variable */
	auto int no_newline = FALSE;	/* don't put out trailing newline */
	auto int start = 1;		/* starting argument number */

	if (argc > 1)
	{
		if (strcmp (argv[1], "-n") == 0)
		{
			start++;
			no_newline = TRUE;
		}

		for (i = start; i <= argc - 1; i++)
		{
			if (i > start)
				putchar (' ');
			printf ("%s", argv[i]);
		}
	}
	if (! no_newline)
		putchar ('\n');
	exit (0);
}
-------------------------------------
(* echo : echo out command line arguments *)

program echo (output);
var
	i : integer;		(* loop control variable *)
	nonewline : boolean;	(* don't put out trailing newline *)
	start : integer;	(* starting argument number *)
	rawtext : packed array [1..100] of char;    (* argument text padded *)
	argtext : packed array [1..100] of char;    (* argument text *)

	procedure fixarg;	(* BSD Pascal pads strings with blanks *)
	var
		i, j : integer;
	begin
		j := 100;
		while rawtext[j] = ' ' do
			j := j - 1;
		for i := 1 to j do
			argtext[i] := rawtext[i];
		for i := j + 1 to 100 do
			argtext[i] := chr(0);
	end;

begin
	nonewline := false;
	start := 1;
	if argc > 1 then
	begin
		argv (start, rawtext);	(* get the argument *)
		if (rawtext = '-n') then begin
			start := start + 1;
			nonewline := true
			end
		else
			fixarg;

		for i := start to argc - 1 do
		begin
			argv (i, rawtext);
			fixarg;
			if i > start then
				write (' ');
			write (argtext)
		end;
	end;
	if not nonewline then
		writeln;
end.
-------------------------------------
-- echo : echo out command line arguments

with text_io;	use text_io;
with u_env;	use u_env;	-- get Unix argc and argv (VADS specific)
procedure ECHO is
	no_newline : boolean := FALSE;	-- don't put out trailing newline
	start : integer := 1;		-- starting argument number
begin
	if (argc > 1) then
		if (argv(1).all = "-n") then
			start := start + 1;
			no_newline := TRUE;
		end if;

		for i in start .. argc - 1 loop
			if (i > start) then
				PUT (" ");
			end if;
			PUT (argv(i).all);
		end loop;
	end if;
	if (not no_newline) then
		NEW_LINE;
	end if;
end ECHO;
-- 
Arnold Robbins
CSNET:	arnold@gatech	ARPA:	arnold%gatech.csnet@csnet-relay.arpa
UUCP:	{ akgua, allegra, hplabs, ihnp4, seismo, ut-sally }!gatech!arnold

When taking aim at the ball, it helps tremendously to pretend it's a PR1ME...