jayferd/rouge — pull request #41 by korny (2012-10-21 17:50:43+00:00)
Title: Performance improvements

Hey, I'm playing around with Rouge right now. It's awesome to have a really good port of Pygments in Ruby.

The only thing it misses right now is comparable speed. In my tests (Ruby 1.9.3 and Python 2.7.2), `rougify` is about 4-7x slower than `pygmentize`.

To help things, I detected two hot spots that are easy to optimize. It should speed up highlighting by about 40%:

```
1.9.3-p194 ~/ruby/rouge:master time ruby -Ilib bin/rougify highlight code.json > /dev/null
real	0m3.437s

1.9.3-p194 ~/ruby/rouge:speedup time ruby -Ilib bin/rougify highlight code.json > /dev/null
real	0m2.077s

1.9.3-p194 ~/ruby/rouge:master time ruby -Ilib bin/rougify highlight code.rb > /dev/null
real	0m12.583s

1.9.3-p194 ~/ruby/rouge:speedup time ruby -Ilib bin/rougify highlight code.rb > /dev/null
real	0m6.885s
```

- `code.rb` is the Rouge code (348K)
- `code.json` is a simple JSON log file (216K)

Maybe we can find more such optimizations. Based on my own experience, I firmly believe that Ruby code can be as fast as Python code without sacrificing too much beauty.