occasionally useful ruby, ubuntu, etc

2Apr/090

re-"require"-ing: how's the speed?

What this is about

So I like the design that, when you need a particular library in your code, you require it in right there in the method/class (JIT library-loading, so to speak), even if it means the require directive will get executed more than once. Running through irb you can tell that calling require twice on the same library is usually much faster the second time, but how much faster?
Read on for benchmarks!

Benchmarks

I came up with some simple benchmarks for a few gems below:

Library Initial load (seconds) Subsequent loads (as multiplier of initial load)
open-uri 0.02 250-325
rack 0.08 2500-2900
rest_client 0.1 3000-4100
activerecord 0.9 270-300
activesupport 0.7 190-200
heroku 0.3 800-900
net/http 0.03 300-450

Example: heroku took 0.3 seconds to load the first time and subsequent attempts to load it were 800-900 times faster.
Note: these are very ballpark figures. I ran most of them 3-5 times and eyeballed the representative range.

Commentary

So what's the takeaway message? One is unless you're concerned about performance, re-requiring isn't going to be what's putting a damper on the speed in your program. On the other hand, you're probably well off not requiring activerecord or activesupport more than once, whereas requiring rest_client you can probably do as often as you want.

Where's the code?

Ah, right, the code. Here's a pastie link for ya.

Filed under: ruby Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.