File: bench_java_invocation.rb

package info (click to toggle)
jruby 1.5.1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze-lts
  • size: 47,024 kB
  • ctags: 74,144
  • sloc: ruby: 398,155; java: 169,506; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (109 lines) | stat: -rw-r--r-- 1,797 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'benchmark'
require 'java'

import java.lang.System

puts "Measure bytelist appends (via Java integration)"
5.times { 
  puts Benchmark.measure { 
    sb = org.jruby.util.ByteList.new 
    foo = org.jruby.util.ByteList.plain("foo") 
    1000000.times { 
      sb.append(foo) 
    } 
  }
}

puts "Measure string appends (via normal Ruby)"
5.times { 
  puts Benchmark.measure { 
    str = "" 
    foo = "foo" 
    1000000.times { 
      str << foo 
    } 
  } 
}

puts "Measure System.currentTimeMillis, int becoming Fixnum"
def System.bench
  x = nil
  1000000.times {
    x = currentTimeMillis
  }
end

5.times {
  puts Benchmark.measure {
    System.bench
  }
}

puts "Measure string.length, integer length to fixnum"
5.times {
  puts Benchmark.measure {
    x = ""
    1000000.times {
      y = x.length
    }
  }
}

puts "Measure java.lang.Thread#name, String entering Ruby"
JThread = java::lang::Thread
class JThread
  def bench
    x = nil
    1000000.times {
      x = name
    }
  end
end

5.times {
  puts Benchmark.measure {
    JThread.currentThread.bench
  }
}

puts "Measure Fixnum#to_s, String being constructed"
5.times {
  puts Benchmark.measure {
    x = 1
    1000000.times {
      y = x.to_s
    }
  }
}

puts "Measure Integer.valueOf, overloaded call with a primitive"
5.times {
  puts Benchmark.measure {
    integer = java.lang.Integer
    x = 1
    1000000.times {
      integer.valueOf(x)
    }
  }
}
    
puts "Measure ArrayList<Object>.get, non-coercible type entering Ruby"
5.times {
  puts Benchmark.measure {
    list = java.util.ArrayList.new
    list.add(java.lang.Object.new)
    1000000.times {
      list.get(0)
    }
  }
}
    
puts "Measure java.lang.Object.new"
5.times {
  puts Benchmark.measure {
    1000000.times {
      Object.new
    }
  }
}