From f4ffdda20043b0f3b6502ab617e39e230d758f25 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sat, 2 Sep 2017 10:44:54 +0200 Subject: [PATCH] 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. --- Awk/hello | 2 + Bash/hello | 2 + C/Makefile | 3 ++ C/hello.c | 6 +++ Dash/hello | 2 + Java/build.xml | 26 +++++++++ Java/hello.jar | Bin 0 -> 1005 bytes Java/hello.sh | 3 ++ Java/manifest.mf | 1 + Java/src/at/hjp/hello/Main.java | 7 +++ Makefile | 7 +++ PHP/hello | 2 + Perl/hello_classic | 6 +++ Perl/hello_modern | 5 ++ Perl/hello_simple | 2 + Python/hello | 2 + README | 1 + Zsh/hello | 2 + Zsh/hello-f | 2 + timethem.c | 93 ++++++++++++++++++++++++++++++++ 20 files changed, 174 insertions(+) create mode 100755 Awk/hello create mode 100755 Bash/hello create mode 100644 C/Makefile create mode 100644 C/hello.c create mode 100755 Dash/hello create mode 100644 Java/build.xml create mode 100755 Java/hello.jar create mode 100755 Java/hello.sh create mode 100644 Java/manifest.mf create mode 100644 Java/src/at/hjp/hello/Main.java create mode 100644 Makefile create mode 100755 PHP/hello create mode 100755 Perl/hello_classic create mode 100755 Perl/hello_modern create mode 100755 Perl/hello_simple create mode 100755 Python/hello create mode 100644 README create mode 100755 Zsh/hello create mode 100755 Zsh/hello-f create mode 100644 timethem.c 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 0000000000000000000000000000000000000000..fbfeab28e1729449130135bdd3bc8a326df90de5 GIT binary patch literal 1005 zcmWIWW@h1HVBlb2Xx_Kkg#ih0GO#fCx`sIFdiuHP|2xIN5CB!m!O#R$DzSZ9Y6Vbv zClHGOFtHTrUE4x$I4^-8Oeh%P+6E-C^*+^wH67c2~s--ODfXoYdKDt=QK!eNm9}j?9d#WvgGjdbj?4zdytA8~hg?L>9E3RomvYMUcCMWmn6wPR$;B)sV{vX9QRm z3!j~!9V}4w@~rDJxv5)L%{VnNenyg&Oph{$3F|gxl^23h&u>MQU#TzRwzd?w(Yc}9 z)@jY2IjO~#+L@I*-`x0@U0yo-rS<>yCRO{sTRo4w!z_IA)NPB(v&ZJW-Fm9M<=reJ z$1|trr}nGFnw$Ju@mciLih@sU3M_xRBs(|O{ot#LM@nfm*wCN=uy1bVEYP;pt zBp=|-$RxsyJJkV$3v14+16q%?_aIkwaR)(*D( zDv$|ID=;fDlM^VlKmf?K-+)Y@21we$tqGKRAONIE8kk6+nqX-OrWJc?f@xmT=!UKp Wk+uT7S=m4e*?=$^sH}tu!~*~y0w$0E literal 0 HcmV?d00001 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; +}