Time hello, world

The classic first program in various languages plus a wrapper to
benchmark them. This is a nice way to measure startup overhead of
language runtimes.
This commit is contained in:
Peter J. Holzer 2017-09-02 10:44:54 +02:00
commit f4ffdda200
20 changed files with 174 additions and 0 deletions

2
Awk/hello Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/awk -f
BEGIN { print "Hello, world!" }

2
Bash/hello Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
echo "Hello, world!"

3
C/Makefile Normal file
View File

@ -0,0 +1,3 @@
all: hello
clean:
rm hello

6
C/hello.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}

2
Dash/hello Executable file
View File

@ -0,0 +1,2 @@
#!/bin/dash
echo "Hello, world!"

26
Java/build.xml Normal file
View File

@ -0,0 +1,26 @@
<project name="hello" default="compile" basedir=".">
<description>
Sieve of Eratosthenes in Java
</description>
<property name="src" location="src"/>
<property name="classes" location="classes"/>
<target name="clean">
<delete dir="classes"/>
</target>
<target name="init">
<tstamp/>
<mkdir dir="classes"/>
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="classes" includeantruntime="false" />
</target>
<target name="jar" depends="compile">
<jar destfile="hello.jar" basedir="classes"
manifest="manifest.mf" />
</target>
</project>

BIN
Java/hello.jar Executable file

Binary file not shown.

3
Java/hello.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
set -x
java -jar ${0%/*}/hello.jar

1
Java/manifest.mf Normal file
View File

@ -0,0 +1 @@
Main-Class: at.hjp.hello.Main

View File

@ -0,0 +1,7 @@
package at.hjp.hello;
class Main {
public static void main(String[] argv) {
System.out.println("Hello, world!");
}
}

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
CFLAGS = -g -Wall -std=gnu11
LDFLAGS = -lrt
timethem:
clean:
rm timethem

2
PHP/hello Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/php
Hello, world!

6
Perl/hello_classic Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/perl
use warnings;
use strict;
print "Hello, world!\n";

5
Perl/hello_modern Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/perl
use Modern::Perl;
say "Hello, world!";

2
Perl/hello_simple Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/perl
print "Hello, world!\n";

2
Python/hello Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/python3
print("Hello, world!")

1
README Normal file
View File

@ -0,0 +1 @@
The classic.

2
Zsh/hello Executable file
View File

@ -0,0 +1,2 @@
#!/bin/zsh
echo "Hello, world!"

2
Zsh/hello-f Executable file
View File

@ -0,0 +1,2 @@
#!/bin/zsh -f
echo "Hello, world!"

93
timethem.c Normal file
View File

@ -0,0 +1,93 @@
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define N 5
typedef struct {
double time;
size_t nread;
} resultT;
int cmptime(const void *a, const void *b) {
const resultT *aa = a, *bb = b;
if (aa->time < bb->time) {
return -1;
} else if (aa->time > bb->time) {
return +1;
} else {
return 0;
}
}
resultT median (resultT *v, int n) {
// n is small, keep it simple
qsort(v, n, sizeof(*v), cmptime);
return v[n / 2];
}
int main(int argc, char **argv) {
resultT **results = calloc(sizeof(*results), argc);
assert(results);
for (int i = 1; i < argc; i++) {
results[i] = calloc(sizeof(**results), N);
assert(results[i]);
}
for (int j = 0; j < N; j++) {
for (int i = 1; i < argc; i++) {
struct timespec t0, t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
size_t nread = 0;
int out[2];
if (pipe(out) == -1) {
printf("%s: cannot create pipe: %s\n", argv[0], strerror(errno));
exit(1);
}
pid_t pid = fork();
switch(pid) {
case -1:
fprintf(stderr, "%s: cannot fork: %s\n", argv[0], strerror(errno));
exit(1);
case 0:
dup2(out[1], 1);
// close(out[0]);
// close(out[1]);
execl(argv[i], argv[i], (char *)NULL);
fprintf(stderr, "%s: cannot exec %s: %s\n", argv[0], argv[i], strerror(errno));
exit(1);
default: {
close(out[1]);
ssize_t n;
char buf[16];
while ((n = read(out[0], buf, sizeof(buf))) > 0) {
nread += n;
}
int status;
while (waitpid(pid, &status, 0) != pid);
}
}
clock_gettime(CLOCK_MONOTONIC, &t1);
results[i][j].time = (t1.tv_sec - t0.tv_sec) * 1E9 + (t1.tv_nsec - t0.tv_nsec);
results[i][j].nread = nread;
fprintf(stderr, "%s: %s completed in %f ns\n", argv[0], argv[i], results[i][j].time);
}
}
for (int i = 1; i < argc; i++) {
resultT m = median(results[i], N);
printf("%-20s %8.3f ms %2d bytes\n", argv[i], m.time / 1E6, m.nread);
}
return 0;
}