public void Time() { Class klass = new Class("NSString"); NSObject str1 = (NSObject) klass.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello world")); NSObject str2 = (NSObject) klass.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("100")); NSObject str3 = (NSObject) klass.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("foo")); Stopwatch timer = Stopwatch.StartNew(); for (int i = 0; i < 10000; ++i) { str3.Call("hasPrefix:", str1); } Console.WriteLine("{0} {1:0.0} secs", new Native(str3, new Selector("hasPrefix:")), timer.ElapsedMilliseconds/1000.0); timer = Stopwatch.StartNew(); for (int i = 0; i < 10000; ++i) { str2.Call("intValue"); } Console.WriteLine("{0} {1:0.0} secs", new Native(str2, new Selector("intValue")), timer.ElapsedMilliseconds/1000.0); timer = Stopwatch.StartNew(); for (int i = 0; i < 10000; ++i) { str1.Call("uppercaseString"); } Console.WriteLine("{0} {1:0.0} secs", new Native(str1, new Selector("uppercaseString")), timer.ElapsedMilliseconds/1000.0); Native native = new Native(str1, new Selector("uppercaseString")); timer = Stopwatch.StartNew(); for (int i = 0; i < 10000; ++i) { native.Invoke(); } Console.WriteLine("Native {0} {1:0.0} secs", native, timer.ElapsedMilliseconds/1000.0); string s = "hello world"; timer = Stopwatch.StartNew(); for (int i = 0; i < 10000; ++i) { s.ToUpper(); } Console.WriteLine("Managed ToUpper: {0:0.0} secs", timer.ElapsedMilliseconds/1000.0); }
internal static object Call(IntPtr instance, string name, object[] args) { Contract.Requires(instance != IntPtr.Zero, "instance is zero"); object result; if (name == "alloc" && args.Length == 0) // need this so we can create an auto release pool without leaking NSMethodSignature { IntPtr exception = IntPtr.Zero; IntPtr ip = DirectCalls.Callp(instance, Selector.Alloc, ref exception); if (exception != IntPtr.Zero) CocoaException.Raise(exception); result = NSObject.Lookup(ip); } else { Selector selector = new Selector(name); MethodSignature sig = new MethodSignature(instance, (IntPtr) selector); Native native = new Native(instance, selector, sig); native.SetArgs(args); result = native.Invoke(); } return result; }