Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
$LOAD_PATH.push './lib'
n = 1000
begin
require 'continuation'
rescue LoadError
# Ignore, Ruby 1.8
end
require_start = Time.new
cc, i = callcc { |cc| [cc, 0 ] }
require 'rxhp'
i += 1
cc.call(cc, i) if i < n
require_end = Time.new
# This actually defines a new kind of element, that is composed of other
# elements - it has no real rendering itself.
class HelloWorldBody < Rxhp::ComposableElement
include Rxhp::Html # Import methods for every tag
def compose
body do
p 'Hello,'
br
yield # Call the children, and embed them here
p(:class => 'bonus') do
text "& <some escaping too>"
end
end
end
end
# Well, you could just create an instance of it (or any other element)
# directly:
# foo = HelloWorldBody.new
# But that would be boring. Let's use a mixin instead (and in the process,
# show that we really did just create a tag, and make it have the missing
# html tag
class Foo
def wrap_a_body
Rxhp::Html.html do # Don't import the methods, fully-qualify them
hello_world_body do
Rxhp::Html.text 'world.'
end
end
end
end
foo = nil
create_start = Time.new
n.times do
foo = Foo.new.wrap_a_body
end
create_end = Time.new
render_start = Time.new
n.times do
foo.render
end
render_end = Time.new
ugly_start = Time.new
n.times do
foo.render(:pretty => false)
end
ugly_end = Time.new
tiny_start = Time.new
n.times do
foo.render(
:pretty => false,
:skip_doctype => true,
:format => Rxhp::TINY_HTML_FORMAT
)
end
tiny_end = Time.new
tiny_no_validate_start = Time.new
n.times do
foo.render(
:pretty => false,
:skip_doctype => true,
:skip_validate => true,
:format => Rxhp::TINY_HTML_FORMAT
)
end
tiny_no_validate_end = Time.new
puts <<EOF
#{n} loads: #{require_end - require_start}s
#{n} creations: #{create_end - create_start}s
#{n} renders: #{render_end - render_start}s
#{n} ugly renders: #{ugly_end - ugly_start}s
#{n} tiny renders: #{tiny_end - tiny_start}s
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.