开发者

multiple if() or database read is faster?

开发者 https://www.devze.com 2023-02-09 01:56 出处:网络
I need in onCreate() read some values based on a value that is stored in SharedPrefrences... So i can have the values that i want after ma开发者_如何学Gony if elseif statements or from reading the val

I need in onCreate() read some values based on a value that is stored in SharedPrefrences... So i can have the values that i want after ma开发者_如何学Gony if elseif statements or from reading the values from the database! Which will be faster? multiple if's or search and read from the db?


The ifs are not the issue in themselves. If they do not call cpu-intensive functions, then they will be very fast, especially on Froyo or later, thanks to the JIT. A read from the DB can be very fast, but can also be very slow. NAND is unpredictable performance-wise, and concurrent I/O will lead to a noticable delay. If you are sure that you do not call heavyweight functions in your ifs, I'd go with them.

The best way would be to wrap the section of code in // start tracing to "/sdcard/calc.trace" Debug.startMethodTracing("calc"); // ... // stop tracing Debug.stopMethodTracing(); (taken from http://developer.android.com/guide/developing/tools/traceview.html). Then get the file from the sdcard, open in Traceview and compare both alternatives. Another, easier way is to Log.d (TAG, "Start: " + System.currentTimeMillis ());, do your code, then Log.d (TAG, "End: " + System.currentTimeMillis ()); Do this for both alternatives and see which one was faster.

Read more on the topic here: http://developer.android.com/guide/practices/design/performance.html


Agree with @kread but here is my notice. As for me reading from the DB shouldn't be performed in onCreate() method as it's an IO operation. It can noticeably block the GUI thread. DB query should me moved to a separate thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消