urbushey
urbushey

Reputation: 1347

How Do You Rapidly Benchmark Scripts

I've seen a lot of blogs where authors discuss throwing together quick benchmark tests, like this Ruby 1.9.0 v Python 2.5.1 that Antonio Cangiano "throws together at 3am."

Is there a simple way to time a script to the millisecond like that that I'm unaware of? Is he probably using built-in functions of OS X or individual libraries? Does Python have a standard lib for this?

How would you do this if you were just going to take the path of least resistance and throw it together at 3am?

Upvotes: 3

Views: 367

Answers (5)

Naveed
Naveed

Reputation: 11167

for ruby benchmarking try this

require 'benchmark'

Benchmark.realtime do #your code,this end

also try measure which give more detail total system time etc

Benchmark.measure do 

end

Upvotes: 1

the Tin Man
the Tin Man

Reputation: 160551

Ruby has a Benchmark module as part of the standard distribution. It's simple to use. A quick search here on Stack Overflow will turn up a lot of samples of its use that I've done.

Upvotes: 1

Andrew Grimm
Andrew Grimm

Reputation: 81510

If you're after mere benchmarking, and you're not too worried about start-up time, and you want it to be programming language independent and you're on Unix, you'd probably use unix time:

time ruby -e "1.upto(10000000) {|i| i}"
real    0m2.926s
user    0m1.570s
sys 0m1.350s

Upvotes: 1

VenkatH
VenkatH

Reputation: 159

I don't know how to do this in Ruby, but in Python I do this -

import time
def my_function():
    a = time.clock()
    //code which you want to benchmark
    b = time.clock()
    print b-a

Obviously, this is a "throw together at 3am benchmark." No frills.

Upvotes: 1

Rob Wouters
Rob Wouters

Reputation: 16327

Take a look at the timeit module.

Upvotes: 5

Related Questions