Guest User

Untitled

a guest
May 28th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. require 'rubygems'
  2. require 'v8'
  3.  
  4. cxt = V8::Context.new
  5.  
  6.  
  7. # context switch test
  8.  
  9. start = Time.now
  10. 1000.times { cxt.eval('true') }
  11. puts "V8 1000 context switches: #{(Time.now - start) * 1000}ms"
  12.  
  13.  
  14.  
  15. # string matching
  16.  
  17. needle = 'bea'
  18. # needle = 'sea'
  19. haystack = 'beatles'
  20. cxt['needle'] = needle
  21. cxt['haystack'] = haystack
  22.  
  23. start = Time.now
  24. 1000.times { haystack.match(needle) }
  25. puts "Ruby 1000 string matches: #{(Time.now - start) * 1000}ms"
  26.  
  27. start = Time.now
  28. cxt.eval('for (i=0;i<1000;i++) { haystack.search(needle) }')
  29. puts "V8 1000 string matches: #{(Time.now - start) * 1000}ms"
  30.  
  31.  
  32.  
  33. # simple comparison
  34.  
  35. start = Time.now
  36. (0..200000).each { |n| (n % 3) == 10 }
  37. puts "Ruby 200000 mods: #{(Time.now - start) * 1000}ms"
  38.  
  39. start = Time.now
  40. cxt.eval('for (i=0;i<200000;i++) { (i % 3) == 0 }')
  41. puts "V8 200000 mods: #{(Time.now - start) * 1000}ms"
  42.  
  43.  
  44.  
  45. # array filter functions
  46.  
  47. big_array = Array(0..200000)
  48. cxt['big_array'] = big_array
  49.  
  50. start = Time.now
  51. matches = big_array.find_all { |n| (n % 1001) == 0 }
  52. puts "Ruby simple matches: #{(Time.now - start) * 1000}ms, count: #{matches.size}"
  53.  
  54. start = Time.now
  55. matches = Array(cxt.eval('big_array.filter(function(n) { return ((n % 1001) == 0) });'))
  56. puts "V8 simple matches: #{(Time.now - start) * 1000}ms, count: #{matches.size}"
Add Comment
Please, Sign In to add comment