EDIT: Added unsafe version of C # code. Thanks guys for this suggestion, unsafe C # code is faster, but only about 3%.
Short version: I wrote some control code in C # and Objective-C and tested it on iPad 3. For MonoTouch / C #, it takes 50% -150% more time to execute than the same code in Objective-C. Here is my question: Can I write C # code that runs faster than the code I used for the test (see below), or is it caused by some inherent MonoTouch / Obj-C difference?
Long version: I wrote a small prototype for a planned multi-platform game. After porting the game core from Windows / .NET to the iPad 3 / MonoTouch, I noticed how much slower the code runs on the iPad. Certain key parts of the kernel core in the iPad processor were 10 times slower than on the Intel processor (which seems normal since the iPad works with the ARM processor).
However, since this is a serious problem for our game, I did a little test on the iPad 3, once with MonoTouch , and then the same thing with simple Objective-C . In the test, there are many simple additions to the float and the search for the float array. Since MonoTouch GC'ed, I expected to see a slight difference in favor of Obj-C, but, to my surprise, MonoTouch code takes much longer to run than Obj-C code. To be precise:
- Obj-C code needs
47'647 ms to run in DEBUG mode and 27'162 ms in RELEASE mode. - MonoTouch code without
unsafe pointers requires 116'885 ms to run in DEBUG mode and 40'002 ms in RELEASE mode. - MonoTouch code with
unsafe pointers needs 90'372 ms to run in DEBUG mode and 38'764 ms in RELEASE mode.
Of course, RELEASE mode is what I care about.
This difference seems a bit big to me, given the fact that MonoTouch compiles the same native LLVM code as Obj-C.
Here is the Obj-C code I used:
int i, j; long time = GetTimeMs64(); float * arr = (float *) malloc(10000 * sizeof(float));
GetTimeMs64() uses <sys/time.h> gettimeofday .
Here is my C # / MonoTouch code in unsafe version:
var array = new float[10000];
EDIT 2: This is the answer we got from Xamarin:
In this particular example, there are two problems that affect mono performance.
First, if you use the default MonoTouch compiler, not LLVM, you can expect lower performance, as it is configured for compilation speed rather than execution speed. LLVM will give you the best results.
Secondly, mono complies with the ECMA specification regarding floating point and all calculations with double precision. This is usually the measured cost of execution, especially when compared to C code using floats.
We studied ways to mitigate double precision without sacrificing correctness, or at least choose a mechanism.