本文介绍了有没有人有基准(code和;的结果)比较写在Xamarin C#和Java的Andr​​oid应用程序的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到Xamarin声称,他们的单声道实现在Android和自己的C#编译应用程序是比Java code更快。对不同的Andr​​oid平台上非常类似于Java和C#code有没有人进行实际的基准测试,以验证这种说法,可以张贴了code和结果?

补充2013年6月18日

由于没有答案,但没有找到这样的基准被别人做了,决定做我自己的测试。不幸的是,我的问题是锁定,所以我不能发布此作为回答,只是编辑的问题。请投票,重新打开了这个问题。对于C#,我用Xamarin.Android版本。 4.7.09001(测试版)。该人士$ ​​C $ C,我的所有用于测试的数据,并编制APK包在GitHub上:

Java的: https://github.com/gregko/TtsSetup_Java

C#: https://github.com/gregko/TtsSetup_C_sharp

如果有人想重复我的测试中的其他设备或模拟器,我有兴趣去学习,结果也是如此。

从我的测试结果

我移植我一句提取级到C#(从我@Voice大声阅读器应用程序),运行在10 HTML文件的一些测试,英语,俄语,法语,波兰语和捷克语语言。每次运行进行的所有10个文件5倍,总时间为3个不同的设备和一个仿真器下面张贴。我测试版本只有建立,不启用调试。

HTC的Nexus One的Andr​​oid 2.3.7(API 10) - 的CyanogenMod ROM

Java的:总计时间(5道):12361毫秒,以读取文件总数:13304毫秒

C#:总计时间(5道):17504毫秒,以读取文件总数:17956毫秒

三星Galaxy S2 SGH-I777(安卓4.0.4,API 15) - 的CyanogenMod ROM

Java的:总计时间(5道):8947毫秒,与文件读取总:9186毫秒

C#:总计时间(5道):9884毫秒,与文件读取总:10247毫秒

三星GT-N7100(安卓4.1.1软糖,API 16) - 三星ROM

Java的:总计时间(5道):9742毫秒,与文件读取总:10111毫秒

C#:总计时间(5道):10459毫秒,以读取文件总数:10696毫秒

仿真器 - 英特尔(安卓4.2,API 17)

Java的:总计时间(5道):2699毫秒,与文件读取总:3127毫秒

C#:总计时间(5道):2049毫秒,与文件读取总:2182毫秒

仿真器 - 英特尔(安卓2.3.7,API 10)

Java的:总计时间(5道):2992毫秒,与文件读取总:3591毫秒

C#:总计时间(5道):2049毫秒,与文件读取总:2257毫秒

仿真器 - ARM(安卓4.0.4,API 15)

Java的:总计时间(5道):41751毫秒,以读取文件总数:43866毫秒

C#:总计时间(5道):44136毫秒,以读取文件总数:45109毫秒

浅谈

我的测试code主要包含文本解析,替换和正则表达式搜索,也许是其他code(例如更多的数字运算)的结果会有所不同。在具有ARM处理器的所有设备,Java的性能比Xamarin C#code更好。最大的不同是Android 2.3的,其中C#code运行在大约下。 70%的Java的速度。

在英特尔仿真器(与英特尔HAX技术,快速的virt仿真模式运行),Xamarin C#code运行我的示例code比Java快得多 - 约1.35时间快。也许单声道虚拟机code和库更好的优化,英特尔比ARM?

修改2013年7月8日

我刚安装Genymotion Android模拟器,它运行在甲骨文VirtualBox的,并再次这一次使用本机的英特尔处理器,而不是模仿的ARM处理器。与英特尔HAX模拟器,再次C#在这里运行得更快。下面是我的结果:

Genymotion仿真器 - 英特尔(安卓4.1.1,API 16)

后来我发现,有一个更新Xamarin.Android测试版,4.7.11版,与发行说明提的单声道运行时的一些变化,以及。决定快速测试一些ARM设备,以及大惊喜 - C#号改进:

BN角落XD +,AMR(安卓4.0)

哇! C#是现在比Java更好吗?决定重复测试对我的银河注2:

三星Galaxy Note 2 - ARM(安卓4.1.1)

下面的C#似乎只是稍微慢一些,但这些数字给了我一下:为什么时间长于上的Nook HD +,尽管注2有一个更快的处理器?答案:节电模式。在角落,这是禁止的,则注2 - 启用。决定测试与禁用(如启用,这也限制了处理器速度)省电模式:

三星Galaxy Note 2 - ARM(安卓4.1.1),省电残疾人

现在,奇怪的是,C#是比Java在ARM处理器上稍微快一点为好。巨大的进步!

修改2013年7月12日

我们都知道,没有什么比本土code的速度,我很不满意我的一句分离器中的Java或C#的性能,特别是我需要提高它(并因此使其更慢) 。决定重新写在C ++。这里是一个小(即一组文件超过previous测试较小,其他原因)本机与Java的在我的银河注2的速度相比,具有省电模式关闭:

Java的:总计时间(5道):3292毫秒,与文件读取总:3454毫秒

本机大拇指:总计时间(5道):537毫秒,与文件读取总数:657毫秒

本机臂:总计时间(5道):458毫秒,与文件读取总数:587毫秒

看起来像我的特殊的测试,本机code比Java快6〜7倍。警告:不能使用std ::在Android正则表达式类,所以只好写我自己的专用程序寻求段落断裂或HTML标签。我最初的使用正则表达式的PC上相同的code测试中,比Java快约4至5倍。

唷!唤醒原始内存使用char *或再次WCHAR *指针,我立刻感到年轻了20岁! :)

修改2013年7月15日

(请参见下面,用二○一三年七月三十零日编辑,与Dot42更好的结果)

有一定的难度我manged端口我的C#测试Dot42(版本1.0.1.71 Beta版),另外C#平台为Android。 preliminary结果表明,Dot42 code为约3倍速度较慢(3倍),比Xamarin C#(第4.7.11),在英特尔的Andr​​oid模拟器。一个问题是,System.Text.RegularEx pressions类Dot42没有,我用在Xamarin测试的斯普利特()函数,所以我使用java.util.regex中的类,而不是和java.util.regex中。 Pattern.Split(),所以在code这个特殊的地方有这种小的差异。应该不是什么大问题,但。 Dot42编译成Dalvik的(DEX)code,所以它与Java合作在Android本身,并没有从C#与Java类似Xamarin需要昂贵的互操作。

只是为了进行比较,我还对ARM设备上运行测试 - 在这里Dot42 code是唯一的2倍比Xamarin C#慢。下面是我的结果:

HTC的Nexus One的Andr​​oid 2.3.7(ARM)

三星Galaxy Note 2,安卓4.1.1(ARM)

英特尔仿真器,安卓4.2(x86)的

要我来说,这也是有趣的是,Xamarin C#是比Java稍快一些较新的ARM设备上,而速度稍慢于旧的Nexus One。如果有人想运行这些测试为好,请让我知道,我会更新在GitHub上的来源。这将是特别有趣,从与英特尔处理器的一个真正的Andr​​oid设备看到的结果。

更新2013年7月26日

只是一个快速更新,重新编译与最新的基准测试应用程序Xamarin.Android 4.8,同时还与今天发布dot42 1.0.1.72升级 - 之前报道的结果没有显著的变化

对于dot42 更好的结果 -

更新二零一三年七月三十日

重新测试Dot42与我的Java code到C#罗伯特的(从dot42制造商)端口。在对Xamarin完成最初我的C#端口,我更换了一些原生Java类,像ListArray,用List类原产于C#等罗伯特没有我Dot42源$ C ​​$ C,所以他从Java再次移植,并采用原装在这些地方的Java类,有利于Dot42,我猜是因为它运行在Dalvik虚拟机,像Java,而不是单声道,比如Xamarin。现在Dot42效果要好得多。下面是我的测试日志:

我仍然认为Dot42还有很长的路要走。拥有类似Java的类(如ArrayList的),并与他们的良好表现,也使移植从Java code到C#稍微容易些。不过,这是后话,我不会有可能做了很多。我宁愿希望使用现有的C#code(库等),将使用原生的C#类(如表),并且将与Xamarin与当前dot42 code执行速度很慢,而且非常好。

格雷格

解决方案

是啊,Xamarin的Mono虚拟机更IM pressive比谷歌的Dalvik的在Android中使用。我曾与HTC传单和宏碁ICONIA TAB平板电脑的Andr​​oid标杆,通过对Java的Dalvik的单声道的C#端口测试过,用C#实现的Andr​​oid和真正痛击基于Java的Dalvik的。

I came across Xamarin claims that their Mono implementation on Android and their C# compiled apps are faster than Java code. Did anyone perform actual benchmarks on very similar Java and C# code on different Android platforms to verify such claims, could post the code and results?

Added June 18, 2013

Since there was no answer and could not find such benchmarks done by others, decided to do my own tests. Unfortunately, my question remains "locked" so I cannot post this as answer, only edit the question. Please vote to re-open this question. For C#, I used Xamarin.Android ver. 4.7.09001 (beta). The source code, all the data I used for testing, and compiled APK packages are on GitHub:

Java: https://github.com/gregko/TtsSetup_Java

C#: https://github.com/gregko/TtsSetup_C_sharp

If someone would like to repeat my tests on other devices or emulators, I'd be interested to learn the results as well.

Results from my testing

I ported my sentence extractor class to C# (from my @Voice Aloud Reader app) and run some tests on 10 html files in English, Russian, French, Polish and Czech languages. Each run was performed 5 times on all 10 files, and the total time for 3 different devices and one emulator are posted below. I tested "Release" builds only, without debugging enabled.

HTC Nexus One Android 2.3.7 (API 10) - CyanogenMod ROM

Java: Grand total time (5 runs): 12361 ms, with file reading total: 13304 ms

C#: Grand total time (5 runs): 17504 ms, with file reading total: 17956 ms

Samsung Galaxy S2 SGH-I777 (Android 4.0.4, API 15) - CyanogenMod ROM

Java: Grand total time (5 runs): 8947 ms, with file reading total: 9186 ms

C#: Grand total time (5 runs): 9884 ms, with file reading total: 10247 ms

Samsung GT-N7100 (Android 4.1.1 JellyBean, API 16) - Samsung ROM

Java: Grand total time (5 runs): 9742 ms, with file reading total: 10111 ms

C#: Grand total time (5 runs): 10459 ms, with file reading total: 10696 ms

Emulator - Intel (Android 4.2, API 17)

Java: Grand total time (5 runs): 2699 ms, with file reading total: 3127 ms

C#: Grand total time (5 runs): 2049 ms, with file reading total: 2182 ms

Emulator - Intel (Android 2.3.7, API 10)

Java: Grand total time (5 runs): 2992 ms, with file reading total: 3591 ms

C#: Grand total time (5 runs): 2049 ms, with file reading total: 2257 ms

Emulator - Arm (Android 4.0.4, API 15)

Java: Grand total time (5 runs): 41751 ms, with file reading total: 43866 ms

C#: Grand total time (5 runs): 44136 ms, with file reading total: 45109 ms

Brief discussion

My test code contains mainly text parsing, replacing and Regex searches, perhaps for other code (e.g. more numeric operations) the results would be different. On all devices with ARM processors, Java performed better than Xamarin C# code. The largest difference was under Android 2.3, where C# code run at approx. 70% of Java speed.

On Intel emulator (with Intel HAX technology, emulator runs in fast virt mode), Xamarin C# code runs my sample code much faster than Java - about 1.35 time faster. Maybe Mono virtual machine code and libraries are much better optimized on Intel than on ARM?

Edit July 8, 2013

I just installed Genymotion Android emulator, which runs in Oracle VirtualBox, and again this one uses native Intel processor, not emulating ARM processor. As with Intel HAX emulator, again C# runs here much faster. Here are my results:

Genymotion emulator - Intel (Android 4.1.1, API 16)

I then noticed that there was an update to Xamarin.Android beta, version 4.7.11, with release notes mentioning some changes in Mono runtime as well. Decided to quickly test some ARM devices, and big surprise - C# numbers improved:

BN Nook XD+, ARM (Android 4.0)

Wow! C# is now better than Java? Decided to repeat the test on my Galaxy Note 2:

Samsung Galaxy Note 2 - ARM (Android 4.1.1)

Here C# seems to be only slightly slower, but these numbers gave me a pause: Why the time is longer than on Nook HD+, even though Note 2 has a faster processor? The answer: power saving mode. On Nook, it was disabled, on Note 2 - enabled. Decided to test with power saving mode disabled (as with enabled, it also limits the processor speed):

Samsung Galaxy Note 2 - ARM (Android 4.1.1), power saving disabled

Now, surprisingly, C# is slightly faster than Java on ARM processor as well. Big improvement!

Edit July 12, 2013

We all know, that nothing beats native code for speed, and I was not satisfied with the performance of my sentence splitter in Java or C#, particularly that I need to improve it (and thus make it even slower). Decided to re-write it in C++. Here is a small (i.e. a smaller set of files than previous tests, for other reasons) comparison of the speed of native vs. Java on my Galaxy Note 2, with power saving mode disabled:

Java:Grand total time (5 runs): 3292 ms, with file reading total: 3454 ms

Native thumb:Grand total time (5 runs): 537 ms, with file reading total: 657 ms

Native arm:Grand total time (5 runs): 458 ms, with file reading total: 587 ms

Looks like for my particular test, the native code is 6 to 7 times faster than Java. Caveat: could not use std::regex class on Android, so had to write my own specialized routines searching for paragraphs breaks or html tags. My initial tests of the same code on a PC using regex, were about 4 to 5 times faster than Java.

Phew! Waking raw memory with char* or wchar* pointers again, I instantly felt 20 years younger! :)

Edit July 15, 2013

(Please see below, with edits of 7/30/2013, for much better results with Dot42)

With some difficulty I manged to port my C# tests to Dot42 (version 1.0.1.71 beta), another C# platform for Android. Preliminary results show that Dot42 code is about 3x (3 times) slower than Xamarin C# (v. 4.7.11), on an Intel Android emulator. One problem is, that System.Text.RegularExpressions class in Dot42 does not have the Split() function that I used in Xamarin tests, so I used Java.Util.Regex class instead, and Java.Util.Regex.Pattern.Split(), so in this particular place in the code there is this small difference. Should not be a big problem though. Dot42 compiles to Dalvik (DEX) code, so it cooperates with Java on Android natively, does not need expensive interop from C# to Java like Xamarin.

Just for comparison, I also run the test on ARM devices - here Dot42 code is "only" 2x slower than Xamarin C#. Here are my results:

HTC Nexus One Android 2.3.7 (ARM)

Samsung Galaxy Note 2, Android 4.1.1 (ARM)

Intel emulator, Android 4.2 (x86)

To me it was also interesting to note that Xamarin C# is slightly faster than Java on a newer ARM device, and slightly slower on the old Nexus One. If anyone would like to run these tests as well, please let me know and I'll update the sources on GitHub. It would be particularly interesting to see results from a real Android device with Intel processor.

Update 7/26/2013

Just a quick update, re-compiled by benchmark apps with the latest Xamarin.Android 4.8, and also with dot42 1.0.1.72 update released today - no significant changes from the results reported before.

Update 7/30/2013 - better results for dot42

Re-tested Dot42 with Robert's (from dot42 makers) port of my Java code to C#. In my C# port done initially for Xamarin, I replaced some native Java classes, like ListArray, with List class native to C#, etc. Robert did not have my Dot42 source code, so he ported it again from Java and used original Java classes in such places, which benefits Dot42, I guess because it runs in Dalvik VM, like Java, and not in Mono, like Xamarin. Now Dot42 results are much better. Here is a log from my testing:

I still think that Dot42 has a long way to go. Having Java-like classes (e.g. ArrayList) and a good performance with them, would make porting code from Java to C# slightly easier. However, this is something I would not be likely to do a lot. I would rather want to use existing C# code (libraries etc.), which will use native C# classes (e.g. List), and that would perform slowly with the current dot42 code, and very well with Xamarin.

Greg

解决方案

Yeah, Xamarin's Mono virtual machine is more impressive than Google's Dalvik used in Android. I have tested it with HTC Flyer and Acer Iconia Tab tablets to benchmark the C# port of Android through Mono against Java Dalvik, with the C# implementation of Android well and truly trouncing the Java-based Dalvik.

这篇关于有没有人有基准(code和;的结果)比较写在Xamarin C#和Java的Andr​​oid应用程序的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 11:22