Skip to content

Instantly share code, notes, and snippets.

@avar
Created October 17, 2010 13:45
Show Gist options
  • Save avar/630868 to your computer and use it in GitHub Desktop.
Save avar/630868 to your computer and use it in GitHub Desktop.
Benchmark Hello world programs. From http://news.ycombinator.com/item?id=1800220
#!/usr/bin/env perl
use strict;
use warnings;
use autodie qw(:all);
use Benchmark qw(:all);
use File::Temp qw(tempdir);
use File::Spec::Functions qw(catfile);
my $dir = tempdir(CLEANUP => 0);
my $obj_file = emit_c($dir);
cmpthese(500, {
php => sub { system qq[php -r 'echo "Hello, world!\\n";'] },
perl => sub { system qq[perl -e 'print "Hello, world!\\n";'] },
ruby => sub { system qq[ruby -e 'puts "Hello, world!"'] },
python => sub { system qq[python -c 'print("Hello, world!")'] },
lua => sub { system qq[lua -e 'print "Hello, world!"'] },
js => sub { system qq[js -e 'print("Hello, world!")'] },
awk => sub { system qq[awk 'BEGIN { print("Hello, world!") }'] },
clojure => sub { system qq[clojure -e '(println "Hello, world!")'] },
C => sub { system qq[$obj_file] },
shell => sub { system qq[sh -c 'echo "Hello, world!"'] },
emacs => sub { system qq[emacs --no-site-file -Q --batch --eval '(message "Hello, World!")'] },
});
sub emit_c
{
my $dir = shift;
my $program = <<'C';
#include <stdio.h>
int main(void)
{
puts("Hello, world!");
return 0;
}
C
my $f_c = catfile($dir, "hello.c");
my $f_o = catfile($dir, "hello");
open my $fh, ">", $f_c;
print $fh $program;
close $fh;
system "cc -Wall -o $f_o $f_c";
return $f_o;
}
__END__
Rate clojure php emacs python ruby js perl awk lua shell C
clojure 0.844/s -- -97% -97% -98% -99% -99% -99% -99% -99% -99% -100%
php 24.6/s 2812% -- -2% -43% -75% -76% -78% -82% -82% -84% -89%
emacs 25.2/s 2882% 2% -- -41% -74% -75% -78% -81% -82% -84% -88%
python 43.0/s 4995% 75% 71% -- -56% -58% -62% -68% -69% -72% -80%
ruby 96.7/s 11361% 294% 284% 125% -- -5% -15% -28% -29% -38% -55%
js 101/s 11919% 313% 303% 136% 5% -- -11% -24% -26% -35% -53%
perl 114/s 13397% 364% 353% 165% 18% 12% -- -15% -17% -27% -47%
awk 134/s 15743% 444% 431% 211% 38% 32% 17% -- -2% -14% -38%
lua 137/s 16134% 458% 444% 219% 42% 35% 20% 2% -- -12% -36%
shell 156/s 18359% 534% 519% 262% 61% 54% 37% 17% 14% -- -28%
C 216/s 25440% 777% 756% 401% 123% 113% 89% 61% 57% 38% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment