Skip to content

Instantly share code, notes, and snippets.

@alno
Created March 26, 2009 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alno/85970 to your computer and use it in GitHub Desktop.
Save alno/85970 to your computer and use it in GitHub Desktop.
Benchmark for Ruby code highliters gems
# Becnhmark for Ruby code highligters
# Gems: ultraviolet, coderay, syntax
require 'benchmark'
require 'rubygems'
require 'uv'
require 'coderay'
require 'syntax/convertors/html'
ruby_text = <<TXT
def test_hl( x, text, lang )
x.report(lang.to_s + ' ultraviolet:') do
Uv.parse( text, "xhtml", lang.to_s, true, "amy")
end
x.report(lang.to_s + ' coderay:') do
CodeRay.scan( text, lang ).div( :line_numbers => :table, :css => :class, :style => :cycnus )
end
x.report(lang.to_s + ' syntax:') do
Syntax::Convertors::HTML.for_syntax( lang.to_s ).convert( text )
end
end
Benchmark.bm( 30 ) do |x|
test_hl( x, ruby_text, :ruby )
test_hl( x, xml_text, :xml )
end
TXT
xml_text = <<TXT
<channel>
<title>Alno's Blog: C++, Java and Rails</title>
<link>http://blog.alno.name</link>
<description>Заметки о разрaботке на C++, Java и Rails</description>
<pubDate>Tue, 10 Feb 2009 10:59:03 GMT</pubDate>
<generator>http://radiantcms.org/</generator>
<language>ru</language>
<atom:link href="http://blog.alno.name/feed/" rel="self" type="application/rss+xml" />
<item>
<title>Boost::Spirit: Грамматики, функции и замыкания</title>
<link>http://blog.alno.name/2008/12/advanced-boost-spirit/</link>
<comments>http://blog.alno.name/2008/12/advanced-boost-spirit/#comments</comments>
<pubDate>Sun, 28 Dec 2008 07:46:00 GMT</pubDate>
<dc:creator>Alno</dc:creator>
<guid isPermaLink="true">http://blog.alno.name/2008/12/advanced-boost-spirit/</guid>
</item>
</channel>
TXT
def test_hl( x, text, lang )
x.report(lang.to_s + ' ultraviolet:') do
100.times do
Uv.parse( text, "xhtml", lang.to_s, true, "amy")
end
end
x.report(lang.to_s + ' coderay:') do
100.times do
CodeRay.scan( text, lang ).div( :line_numbers => :table, :css => :style, :style => :cycnus )
end
end
x.report(lang.to_s + ' syntax:') do
100.times do
Syntax::Convertors::HTML.for_syntax( lang.to_s ).convert( text )
end
end
end
def html_out( name, text )
File.open(name, 'w') do
f.write '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>'
f.write ''
end
end
#Benchmark.bm( 20 ) do |x|
# test_hl( x, ruby_text, :ruby )
# test_hl( x, xml_text, :xml )
#end
File.open('code_highlight.html', 'w') do |f|
f.write '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" type="text/css" media="screen,projection,print" href="http://ultraviolet.rubyforge.org/css/active4d.css" /><link type="text/css" rel="stylesheet" href="http://syntax.rubyforge.org/stylesheets/ruby.css" />
</head><body>'
f.write Uv.parse( ruby_text, "xhtml", "ruby", false, "active4d")
f.write Uv.parse( xml_text, "xhtml", "xml", false, "active4d")
f.write CodeRay.scan( ruby_text, "ruby" ).div( :line_numbers => false, :css => :style, :style => :cycnus )
f.write CodeRay.scan( xml_text, "xml" ).div( :line_numbers => false, :css => :style, :style => :cycnus )
f.write '<div class="ruby">' + Syntax::Convertors::HTML.for_syntax( "ruby" ).convert( ruby_text ) + '</div>'
f.write '<div class="ruby">' + Syntax::Convertors::HTML.for_syntax( "xml" ).convert( xml_text ) + '</div>'
f.write '</body></html>'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment