Code Monkey home page Code Monkey logo

Comments (10)

getActivity avatar getActivity commented on May 25, 2024

@GuangNian10000 小伙子,你提供一下 Bean 类源码及 Json 内容给我,我复现一下这个问题。

from gsonfactory.

GuangNian10000 avatar GuangNian10000 commented on May 25, 2024

我又试了fastjson更快
try {
// 读取文件内容
val file = File(getDirectory(id), "json.txt")
val fileReader = FileReader(file)
val text = BufferedReader(fileReader).use(BufferedReader::readText)
fileReader.close()
val executionTime = measureTimeMillis {
GsonFactory.getSingletonGson().fromJson(text, Bean::class.java) //700
//Gson().fromJson(text, BookCatalog::class.java) //40
}
Timber.d(executionTime)
} catch (e: Exception) {
e.printStackTrace()
null // 返回 null 或者其他指定的错误值来表示读取失败
}

Bean.txt
json.txt

from gsonfactory.

GuangNian10000 avatar GuangNian10000 commented on May 25, 2024

我在家用电脑模拟器试发现GsonFactory解析用时60毫秒左右,可能是因为之前用的测试机,配置不好。但我切到Gson().fromJson用时基本都在20毫秒以下。
不同手机耗时可能有差异,下周我再用测试机跑跑看

from gsonfactory.

GuangNian10000 avatar GuangNian10000 commented on May 25, 2024

荣耀P20 GsonFactory 耗时100左右, Gson30-50

from gsonfactory.

getActivity avatar getActivity commented on May 25, 2024

GsonFactory-9.2.aar.zip

from gsonfactory.

getActivity avatar getActivity commented on May 25, 2024

小伙子,我最近修改了 Kotlin data class 类的实例化方式,可能和你这个问题有关系,你下载上面的 aar 包试试看是否还有这个问题?

from gsonfactory.

GuangNian10000 avatar GuangNian10000 commented on May 25, 2024

没有明显区别,模拟器跑在70-100毫秒,Gson20左右

from gsonfactory.

getActivity avatar getActivity commented on May 25, 2024

@GuangNian10000 小伙子,我对这个问题进行了复现,核实了确实框架存在此问题,GsonFactory 比直接使用 Gson 实例化要耗时,这部分耗时主要来源于反射 kotlin data class 类,在经过了大量的测试和验证后,发现这个问题属于正常的耗时,这是因为框架内部引用了 kotlin 反射库来创建对象,第一次反射创建 kotlin data class 类耗时在 100 多毫秒,后面反射创建耗时基本在 1 毫秒以内(反射创建 2 ~ 3 个对象在 1 毫秒),加上 Json 数据量大,所以导致反射耗时也随时增加。如果直接换成 Gson 来做,确实没有这个问题,但是有另外的问题,kotlin data class 类字段上面的默认值将无法被保留,这个也是框架要使用 kotlin 反射库来反射创建 kotlin 类的原因,如果不使用此方式将无法实现该需求。

经过认真思考和斟酌后,决定不处理该问题,这是因为 kotlin data class 保留默认值的需求要比大数据解析耗时长优先级要高,里面的原因也很简单,前者的需求量比后者大,并且前者的需求比较常见,后者的需求比较少见。

from gsonfactory.

GuangNian10000 avatar GuangNian10000 commented on May 25, 2024

好的谢谢

from gsonfactory.

NBXXF avatar NBXXF commented on May 25, 2024

问题描述【必填】

我分别使用GsonFactory.getSingletonGson().fromJson(str,type)和Gson().fromJson(str,type)来解析文件大小为 300kb 的 json 字符串,前者耗时 700-800 毫秒,后者100 毫秒不到。是我使用的方式不对吗?还是因为框架为了适配做了很多处理的缘故。 版本 9.2

框架文档是否提及了该问题【必答】

是否已经查阅框架文档但还未能解决的【必答】

issue 列表中是否有人曾提过类似的问题【必答】

是否已经搜索过了 issue 列表但还未能解决的【必答】

你可以尝试 https://github.com/NBXXF/gson_plugin

from gsonfactory.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.