public
Description: A lean-and-mean Ruby/ObjC bridge
Homepage: http://rubyobjc.com
Clone URL: git://github.com/timburks/rubyobjc.git
rubyobjc / test / test_speed.rb
100644 74 lines (62 sloc) 1.437 kb
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
#
# test_speed.rb
#
# Tests RubyObjC performance optimizations.
#
# Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
# For more information about this file, visit http://www.rubyobjc.com.
#
 
require 'test/unit'
require 'test/testspeed'
 
require 'lib/objc'
 
TEST_RUBYCOCOA = false
require 'osx/cocoa' if TEST_RUBYCOCOA
 
COUNT = 100000
 
class RubyTester
  def self.add_to_(x,y)
    x+y
  end
end
 
class TestSpeed < Test::Unit::TestCase
 
  def test_rubyobjc
    tester = ObjC::Tester.alloc.init
    t0 = Time.now
    COUNT.times do
      tester.add_to_(2,2)
    end
    t1 = Time.now
    print "#{COUNT} method calls in #{t1 - t0}s"
    assert_equal 4, tester.add_to_(2,2)
  end
 
  def test_ruby
    t0 = Time.now
    COUNT.times do
      RubyTester.add_to_(2,2)
    end
    t1 = Time.now
    print "#{COUNT} method calls in #{t1 - t0}s"
    assert_equal 4, RubyTester.add_to_(2,2)
  end
 
  if TEST_RUBYCOCOA
    def test_rubycocoa
      tester = OSX::Tester.alloc.init
      t0 = Time.now
      COUNT.times do
        tester.add_to_(2,2)
      end
      t1 = Time.now
      print "#{COUNT} method calls in #{t1 - t0}s"
      assert_equal 4, tester.add_to_(2,2)
    end
  end
 
  def test_wrap
    add = ObjC::Function.wrap("add", "i", ["i", "i"])
    t0 = Time.now
    COUNT.times do
      add.call(2, 3)
    end
    t1 = Time.now
    print "#{COUNT} function calls in #{t1 - t0}s"
    assert_equal 5, add.call(2, 3)
  end
 
end