commit f4ffdda20043b0f3b6502ab617e39e230d758f25 Author: Peter J. Holzer Date: Sat Sep 2 10:44:54 2017 +0200 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. diff --git a/Awk/hello b/Awk/hello new file mode 100755 index 0000000..8c3b454 --- /dev/null +++ b/Awk/hello @@ -0,0 +1,2 @@ +#!/usr/bin/awk -f +BEGIN { print "Hello, world!" } diff --git a/Bash/hello b/Bash/hello new file mode 100755 index 0000000..2b70e2c --- /dev/null +++ b/Bash/hello @@ -0,0 +1,2 @@ +#!/bin/bash +echo "Hello, world!" diff --git a/C/Makefile b/C/Makefile new file mode 100644 index 0000000..b312ea3 --- /dev/null +++ b/C/Makefile @@ -0,0 +1,3 @@ +all: hello +clean: + rm hello diff --git a/C/hello.c b/C/hello.c new file mode 100644 index 0000000..3f67e00 --- /dev/null +++ b/C/hello.c @@ -0,0 +1,6 @@ +#include + +int main(void) { + printf("Hello, world!\n"); + return 0; +} diff --git a/Dash/hello b/Dash/hello new file mode 100755 index 0000000..e56e9b7 --- /dev/null +++ b/Dash/hello @@ -0,0 +1,2 @@ +#!/bin/dash +echo "Hello, world!" diff --git a/Java/build.xml b/Java/build.xml new file mode 100644 index 0000000..ad17002 --- /dev/null +++ b/Java/build.xml @@ -0,0 +1,26 @@ + + + Sieve of Eratosthenes in Java + + + + + + + + + + + + + + + + + + + + + + diff --git a/Java/hello.jar b/Java/hello.jar new file mode 100755 index 0000000..fbfeab2 Binary files /dev/null and b/Java/hello.jar differ diff --git a/Java/hello.sh b/Java/hello.sh new file mode 100755 index 0000000..a266f29 --- /dev/null +++ b/Java/hello.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -x +java -jar ${0%/*}/hello.jar diff --git a/Java/manifest.mf b/Java/manifest.mf new file mode 100644 index 0000000..2ef014d --- /dev/null +++ b/Java/manifest.mf @@ -0,0 +1 @@ +Main-Class: at.hjp.hello.Main diff --git a/Java/src/at/hjp/hello/Main.java b/Java/src/at/hjp/hello/Main.java new file mode 100644 index 0000000..8885383 --- /dev/null +++ b/Java/src/at/hjp/hello/Main.java @@ -0,0 +1,7 @@ +package at.hjp.hello; + +class Main { + public static void main(String[] argv) { + System.out.println("Hello, world!"); + } +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2351494 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +CFLAGS = -g -Wall -std=gnu11 +LDFLAGS = -lrt + +timethem: + +clean: + rm timethem diff --git a/PHP/hello b/PHP/hello new file mode 100755 index 0000000..679ad0e --- /dev/null +++ b/PHP/hello @@ -0,0 +1,2 @@ +#!/usr/bin/php +Hello, world! diff --git a/Perl/hello_classic b/Perl/hello_classic new file mode 100755 index 0000000..e53d226 --- /dev/null +++ b/Perl/hello_classic @@ -0,0 +1,6 @@ +#!/usr/bin/perl +use warnings; +use strict; + +print "Hello, world!\n"; + diff --git a/Perl/hello_modern b/Perl/hello_modern new file mode 100755 index 0000000..2991938 --- /dev/null +++ b/Perl/hello_modern @@ -0,0 +1,5 @@ +#!/usr/bin/perl +use Modern::Perl; + +say "Hello, world!"; + diff --git a/Perl/hello_simple b/Perl/hello_simple new file mode 100755 index 0000000..8371027 --- /dev/null +++ b/Perl/hello_simple @@ -0,0 +1,2 @@ +#!/usr/bin/perl +print "Hello, world!\n"; diff --git a/Python/hello b/Python/hello new file mode 100755 index 0000000..0cdacc3 --- /dev/null +++ b/Python/hello @@ -0,0 +1,2 @@ +#!/usr/bin/python3 +print("Hello, world!") diff --git a/README b/README new file mode 100644 index 0000000..c76500d --- /dev/null +++ b/README @@ -0,0 +1 @@ +The classic. diff --git a/Zsh/hello b/Zsh/hello new file mode 100755 index 0000000..06a1db3 --- /dev/null +++ b/Zsh/hello @@ -0,0 +1,2 @@ +#!/bin/zsh +echo "Hello, world!" diff --git a/Zsh/hello-f b/Zsh/hello-f new file mode 100755 index 0000000..406800e --- /dev/null +++ b/Zsh/hello-f @@ -0,0 +1,2 @@ +#!/bin/zsh -f +echo "Hello, world!" diff --git a/timethem.c b/timethem.c new file mode 100644 index 0000000..d28a797 --- /dev/null +++ b/timethem.c @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#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; +}