weikipeng / fanfoudroid

Automatically exported from code.google.com/p/fanfoudroid
0 stars 0 forks source link

SimpleDateFormat导致首次刷新缓慢问题 #87

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
在安能饭否第一次使用 SimpleDateFormat 时, 
会耗大量的时候去读取系统所有时区信息, 这是 SimpleDateFormat 
的固有设计, 其耗费的时间由机器的性能决定. 
虚拟机测试是5秒左右.

Android对此问题已进行的修复是: 
在系统启动时预读该手机设置中的默认时区信息(在手机设置-
语言), 各应用程序则不需要做此耗时操作, 
会直接读取缓存时区信息.

问题是国内的用户会将自己的手机语言设置为中文(Locale.zh_CN)
, 所以Android在启动时缓存的就是中文时区信息, 
故只有在将使用 SimpleDateFormat(format, Locale.CHINA) 
时才会命中缓存.

目前没有太好的解决办法, 要么是在程序启动时(如果空闲), 
则模拟Android系统一样进行一次对 Locale.US 信息的统一预读, 
以防止在HTTP请求时显得缓慢, 但实际意义不大.

不过在今后的Android版本中将不存在此问题, 
开发者给出的回复是:

------------------------------------
this is actually better still in IceCreamSandwich. there i cache en_US plus the 
device's default locale, plus an LRU cache of other locales. so each 
application will have to pay once if the user switches locale (since the zygote 
can't help them then), but the per-app caches should make that less painful.
------------------------------------
来源: http://code.google.com/p/android/issues/detail?id=3147

== Log 分析 ==

.log1
Locale: Locale.ENGLSH
Settings: Lanuage english(US), Zone: CHINA
System: first boot

-------------------------------------------
05-12 11:45:54.045: INFO/DEBUG(386): Debuger [time ={GET FORMATER=20, 
_TOTAL=9681}]
05-12 11:45:54.055: INFO/DEBUG(386): Debuger [time ={GET FORMATER=0, _TOTAL=2}]
05-12 11:45:54.055: INFO/DEBUG(386): Debuger [time ={GET FORMATER=0, _TOTAL=2}]
05-12 11:45:54.085: INFO/DEBUG(386): Debuger [time ={GET FORMATER=0, _TOTAL=4}]
-------------------------------------------

.log2
Locale: Locale.ENGLSH
Settings: Language english(US), Zone: CHINA
System: kill pid

-------------------------------------------
05-12 11:54:27.845: INFO/DEBUG(606): Debuger [time ={GET FORMATER=21, 
_TOTAL=22271}]
05-12 11:54:27.885: INFO/DEBUG(606): Debuger [time ={GET FORMATER=0, _TOTAL=3}]
05-12 11:54:27.926: INFO/DEBUG(606): Debuger [time ={GET FORMATER=0, _TOTAL=37}]
05-12 11:54:27.936: INFO/DEBUG(606): Debuger [time ={GET FORMATER=0, _TOTAL=3}]
-------------------------------------------

.log3
Locale: Locale.ENGLSH
Settings: language english(US), Zone: CHINA
System: reboot

-------------------------------------------
05-12 12:05:27.432: INFO/DEBUG(410): Debuger [time ={GET FORMATER=9, 
_TOTAL=12886}]
05-12 12:05:27.482: INFO/DEBUG(410): Debuger [time ={GET FORMATER=0, _TOTAL=5}]
-------------------------------------------

.log4 [Cache命中]
Local: Locale.US
Settings: Language english(US), Zone: CHINA
System. first boot

----------------------------------------------
05-12 12:15:56.829: INFO/DEBUG(419): Debuger [time ={GET FORMATER=389, 
_TOTAL=466}]
05-12 12:15:56.859: INFO/DEBUG(419): Debuger [time ={GET FORMATER=0, _TOTAL=1}]
05-12 12:15:56.878: INFO/DEBUG(419): Debuger [time ={GET FORMATER=0, _TOTAL=4}]
05-12 12:15:56.900: INFO/DEBUG(419): Debuger [time ={GET FORMATER=0, _TOTAL=14}]
05-12 12:15:56.900: INFO/DEBUG(419): Debuger [time ={GET FORMATER=0, _TOTAL=2}]
----------------------------------------------

.log4 [未命中]
Local: Locale.US
Settings: Language 中文简体, Zone: CHINA
System. first boot

----------------------------------------------
05-12 12:21:03.880: INFO/DEBUG(408): Debuger [time ={GET FORMATER=4, 
_TOTAL=18374}]
05-12 12:21:04.080: INFO/DEBUG(408): Debuger [time ={GET FORMATER=0, _TOTAL=3}]
05-12 12:21:40.491: INFO/DEBUG(408): Debuger [time ={GET FORMATER=0, _TOTAL=5}]
05-12 12:21:40.521: INFO/DEBUG(408): Debuger [time ={GET FORMATER=0, _TOTAL=25}]
----------------------------------------------

== Profile 分析 ==

将服务器响应解析为Status时, 
SimpleDateFormat会读取系统中所有时区信息, 
耗时4~5秒左右.(如单次请求耗7秒, HTTP在2秒内返回响应数据, 
剩下的5秒的时间都在解析时间.)

将 Locale 参数改为与系统默认设置一致可命中缓存.

参考资料: http://code.google.com/p/android/issues/detail?id=3147

[file] TwitterActivity_20110511_2017.trace 
TwitterActivity.addMessages 454.314 [数据库操作]
               .getMessageSinceId 7234.184
Weibo.getFriendsTimeline 7230.374
     .get                1111.533 [HTTP请求]
     Status.constructStatuses  6112.534  [SLOW]
     WeiboResponse.parseDate
     SimpleDateFormat.parse 5223.079
     DateFormatSymbols.internalZoneStrings 4747.606
     [更改] 改为Locale.US
     .get 1296.809
     Status.constructStatuses 1289.832

[file] TwitterActivity_20110511_.trace 
Weibo.getFriendsTimeline 1836.587
     .get                1141.918 [HTTP请求]
     Status.constructStatuses 677.524
     WeiboResponse.parseDate
     SimpleDateFormat.parse 106.273
     无internalZoneStrings, [cache命中]

Original issue reported on code.google.com by lds1...@gmail.com on 12 May 2011 at 5:26