Dramatically Speeding Up Ruby 1.9.3
After reinstalling Ruby 1.9.3 recently, it felt a lot slower than it was previously. Thankfully, I stumbled upon some blog posts that deal with this issue. Check these out:
http://spin.atomicobject.com/2012/11/06/is-your-application-running-with-ruby-slow/
http://alisnic.net/blog/making-your-ruby-fly/
I decided to give it a go. I didn’t compile ruby with the falcon patch, just stock ruby 1.9.3 with the CFLAGS set. I put the following into my ~/.rvmrc file:
rvm_configure_env=(CFLAGS="-march=nocona -O3 -fomit-frame-pointer -pipe")
(the march of nocona is for my computers CPU. You will need to find the correct one for your CPU or compiling will fail)
Then I ran ‘rvm reinstall 1.9.3’. Here are the results (the times are the lowest out of three runs of each command):
% time ruby -e "count = 0; while(count < 100000000); count = count + 1; end; puts count"
Before: 7.5s
After: 3.2s
% time bundle exec rails runner 'true'
Before: 12.4s
After: 6.6s
% time bundle exec rake routes
Before: 12.5s
After: 6.8s
% time bundle exec rspec spec
Before: 5m 53s
After: 3m 17s
Conclusion
If you haven’t compiled Ruby with optimizations, you’re unnecessarily slowing your application.
Updated 26th Nov 2012
I followed the additional advice in the references above and tried the falcon patch. The results are impressive. Check out these comparisons between just CFLAGS and CFLAGS+falcon patch.
% time ruby -e "count = 0; while(count < 100000000); count = count + 1; end; puts count"
Before: 3.2s
After: 3.2s
% time bundle exec rails runner 'true'
Before: 6.6s
After: 4.9s
% time bundle exec rake routes
Before: 6.8s
After: 5.0s
% time bundle exec rspec spec
Before: 3m 17s
After: 2m 57s
Conclusion
Set the CFLAGS for your system and compile ruby 1.9.3 with the falcon patch. You’ll save yourself a heaps of time :-)