Measure time and memory consumption. copied from http://www.update.uu.se/~johanb/memtime/