Skip to content

Instantly share code, notes, and snippets.

@kasima
Created June 11, 2010 20:41
Show Gist options
  • Save kasima/435005 to your computer and use it in GitHub Desktop.
Save kasima/435005 to your computer and use it in GitHub Desktop.
V8 speed tests
require 'rubygems'
require 'v8'
cxt = V8::Context.new
# context switch test
start = Time.now
1000.times { cxt.eval('true') }
puts "V8 1000 context switches: #{(Time.now - start) * 1000}ms"
# string matching
needle = 'bea'
# needle = 'sea'
haystack = 'beatles'
cxt['needle'] = needle
cxt['haystack'] = haystack
start = Time.now
1000.times { haystack.match(needle) }
puts "Ruby 1000 string matches: #{(Time.now - start) * 1000}ms"
start = Time.now
cxt.eval('for (i=0;i<1000;i++) { haystack.search(needle) }')
puts "V8 1000 string matches: #{(Time.now - start) * 1000}ms"
# simple comparison
start = Time.now
(0..200000).each { |n| (n % 3) == 10 }
puts "Ruby 200000 mods: #{(Time.now - start) * 1000}ms"
start = Time.now
cxt.eval('for (i=0;i<200000;i++) { (i % 3) == 0 }')
puts "V8 200000 mods: #{(Time.now - start) * 1000}ms"
# array filter functions
big_array = Array(0..200000)
cxt['big_array'] = big_array
start = Time.now
matches = big_array.find_all { |n| (n % 1001) == 0 }
puts "Ruby simple matches: #{(Time.now - start) * 1000}ms, count: #{matches.size}"
start = Time.now
matches = Array(cxt.eval('big_array.filter(function(n) { return ((n % 1001) == 0) });'))
puts "V8 simple matches: #{(Time.now - start) * 1000}ms, count: #{matches.size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment