1. Miki Tebeka
  2. startime

Source

startime / run.py

#!/usr/bin/env python

from time import time
from subprocess import call
from os import environ, stat
from stat import S_IXUSR
from os.path import isfile, join

def which(name):
    for path in environ["PATH"].split(":"):
        full = join(path, name)
        if not isfile(full):
            continue
        if stat(full).st_mode & S_IXUSR:
            return full


def script(name, *command):
    full = which(name)
    assert full, "can't find %s" % name

    command = list(command) if command else ["/dev/null"]

    return { "name" : name.title(), "command" : [full] + command}

scripts = [
    script("python"),
    script("ruby"),
    script("perl"),
    script("newlisp", "-e", "(exit)"),
    script("clisp", "-q", "-x", "(exit)"),
    script("guile", "-c", ""),
    script("mzscheme", "-e", ""),
]

def timeit(command):
    start = time()
    with open("/dev/null", "wb") as fo:
        retval = call(command, shell=0, stdout=fo)
    end = time()

    assert retval == 0, "%s" % " ".join(command)

    return end - start

def run():
    runtimes = []
    for script in scripts:
        start = time()
        runtimes.append((timeit(script["command"]), script["name"]))

    maxlen = max((len(rt[1]) for rt in runtimes)) + 1
    runtimes.sort()
    for runtime, name in runtimes:
        print "%-*s: %f" % (maxlen, name, runtime)

if __name__ == "__main__":
    run()