安卓:Gson 性能

发布于 2024-09-10 06:39:23 字数 909 浏览 3 评论 0原文

我正在尝试使用 gson 在 android 模拟器上进行对象映射。

处理 208 kb 左右的 json 数据时,速度慢得离谱。我的 json 中没有任何层次结构。

对象映射完成后,我可以看到 gson 创建了大约 500 条记录。

在 Android 模拟器上映射输入 json 需要 3 分钟多的时间。

我已经注释了我的实体,它由字符串和几个浮点数组成。

我错过了什么吗?

任何想法、最佳实践都会有很大帮助。

有什么方法可以快速对象映射 json 数据吗?

        URL myURL = new URL(url);
        /* Open a connection to that URL. */
        URLConnection ucon = myURL.openConnection();
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        InputStreamReader reader = new InputStreamReader(is);
        long tickCount = System.currentTimeMillis();
        Policy[] policies = new Gson().fromJson(reader, Policy[].class);
        long endCount = System.currentTimeMillis() - tickCount;
        Log.d("Time to pull policies in milliseconds", "" + endCount);

I am trying to use gson to do my object mapping on the android emulator.

It has been ridiculously slow when processing json data around 208 kb. I do not have any hierarchies in my json.

After the object mapping is done, i can see it that gson created around 500 records.

It is taking it over 3 minutes on the android emulator to map the input json.

I have annotated my entity which comprises of strings and couple of floats.

An I missing something?

Any ideas, best practices would greatly help.

Are there any ways of quickly object mapping the json data?

        URL myURL = new URL(url);
        /* Open a connection to that URL. */
        URLConnection ucon = myURL.openConnection();
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        InputStreamReader reader = new InputStreamReader(is);
        long tickCount = System.currentTimeMillis();
        Policy[] policies = new Gson().fromJson(reader, Policy[].class);
        long endCount = System.currentTimeMillis() - tickCount;
        Log.d("Time to pull policies in milliseconds", "" + endCount);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉宸 2024-09-17 06:39:23

I've seen questions like this come up before, and the general consensus is that Jackson is much faster than Gson. See the following links for more information:

Here is one which specifically discusses Android: http://ubikapps.net/?p=525

嘿看小鸭子会跑 2024-09-17 06:39:23

您是否尝试过将 GSON 流解析器与 Gson 对象混合? http://sites.google.com/site/gson/streaming(查找混合读取示例)。

这种方法可能会有所帮助,因为 Gson 读取整个解析树,然后对其进行操作。对于大型数组列表,读取所有元素并尝试解析可能会导致大量内存交换(或颠簸)。这种方法将一次读入一个元素。

希望这有帮助。

Have you tried the mixing the GSON streaming parser with the Gson object? http://sites.google.com/site/gson/streaming (look for the Mixed read example).

This approach may help since Gson reads in an entire parse tree and then acts on it. With a large array list, reading in all elements and attempting to parse may cause lot of memory swaps (or thrashing). This approach will read in one element at a time.

Hope this helps.

强辩 2024-09-17 06:39:23

如果您将 InputStream 包装在一个具有良好大缓冲区的 BufferedInputStream 中,您可能会获得更好的性能...

3 分钟太疯狂了。我很少运行模拟器,但我有一个带有约 1.1MB JSON 资源的应用程序,在硬件上加载和处理大约需要 5 秒。

(这仍然太长,但仍然如此)。

You'd probably get better performance if you wrapped that InputStream in a BufferedInputStream with a nice big buffer...

3 minutes is insane. I seldom run the emulator but I have an app with a ~1.1MB JSON asset and that takes around 5 seconds to load and process on hardware.

(Which is still far too long, but still).

柳若烟 2024-09-17 06:39:23

我发现,通过不对 JSON 中不需要的所有元素进行建模,可以显着加快 gson.fromJSON 的速度。 GSON 很乐意只填写您的响应类中指定的内容。

I've found that I can speed up gson.fromJSON quite considerably by not modelling all the elements in the JSON that I won't need. GSON will happily fill in only what is specified in your response classes.

停顿的约定 2024-09-17 06:39:23

我发现创建 Gson 实例是一个非常昂贵的操作,无论是在 CPU 使用还是内存分配方面。

由于 Gson 实例是线程安全的,因此构造和重用单个静态实例是有回报的,特别是当您经常进行序列化/反序列化时。

I have found that CREATING a Gson instance is a very expensive operation, both in terms of CPU used and memory allocated.

Since Gson instances are thread-safe, constructing and reusing a single static instance pays off, especially if you are serializing / deserializing often.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文