简体   繁体   English

Xamarin Java.Lang.Object创建效率低下

[英]Xamarin Java.Lang.Object creation is inefficient

I need to generate a large list of objects for a ListView / GridView. 我需要为ListView / GridView生成大量对象。 From what I understand, these objects must derive from Java.Lang.Object. 据我了解,这些对象必须派生自Java.Lang.Object。 The time it takes to generate these objects is significant. 生成这些对象所花费的时间很长。

I created a simple test to see how long it takes to simply create 5000 objects that derive from Java.Lang.Object and compared it to creating 5000 objects that derive from System.Object. 我创建了一个简单的测试,以查看简单地创建从Java.Lang.Object派生的5000个对象需要花费多长时间,并将其与创建从System.Object派生的5000个对象进行比较。

public class MyJavaObject : Java.Lang.Object { }    
public class MyObject : System.Object { }

private void CreateObjects()
{
    var objectCount = 5000;

    var javaObjectsWatch = new Stopwatch();
    javaObjectsWatch.Start();
    for (int i = 0; i < objectCount; i++)
    {
        new MyJavaObject();
    }
    javaObjectsWatch.Stop();
    var javaObjectsCreationElapsed = javaObjectsWatch.Elapsed.TotalMilliseconds;
    Console.WriteLine($"{objectCount} java objects took {javaObjectsCreationElapsed} milliseconds");

    var objectsWatch = new Stopwatch();
    objectsWatch.Start();
    for (int i = 0; i < objectCount; i++)
    {
        new MyObject();
    }
    objectsWatch.Stop();
    var objectsCreationElapsed = objectsWatch.Elapsed.TotalMilliseconds;
    Console.WriteLine($"{objectCount} objects took {objectsCreationElapsed} milliseconds");
}

Running CreateObjects results in the following being written to console. 运行CreateObjects会将以下内容写入控制台。

"5000 java objects took 4437.1033 milliseconds" “ 5000个Java对象花费了4437.1033毫秒”

"5000 objects took 1.831 milliseconds" “ 5000个对象耗时1.831毫秒”

It takes almost 4.5 seconds to create the MyJavaObjects, and not even a hundredth of a second for the MyObjects. 创建MyJavaObjects大约需要4.5秒,而MyObjects甚至不需要百分之一秒。

How can I get the creation of the MyJavaObjects to be closer to the efficiency of creating MyObjects? 如何获得MyJavaObjects的创建,使其更接近创建MyObjects的效率?

The creation of a Java object contains the parts: 1) the object is created on Dalvik VM; Java对象的创建包含以下部分:1)在Dalvik VM上创建对象; 2) a corresponding object created on Xamarin.Android VM to track the lifecycle of the other object. 2)在Xamarin.Android VM上创建的相应对象,用于跟踪其他对象的生命周期。 Quite similar to .NET/COM inter-operation. 与.NET / COM互操作非常相似。

Thus, it won't be as quick as you wished, but still fast enough for most of the mobile apps. 因此,它的速度不会像您希望的那样快,但是对于大多数移动应用程序来说仍然足够快。 It is a burden the solution must pay when it needs to use a set of specific components. 当解决方案需要使用一组特定的组件时,这是一个负担。 For other scenarios, Xamarin.Android can directly call into Android native API without touching Dalvik. 对于其他情况,Xamarin.Android可以直接调用Android本机API,而无需触摸Dalvik。

Some of the internals can be found at Miguel de Icaza's blog on Mono for Android. 一些内部信息可以在Miguel de Icaza在Android版Mono上的博客中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM