TL; Dr,滾動到最後的編輯。我將這一切都留在這裏,讓人們可以從這個討論的演變中讀到。如果在編譯/反編譯後返回null if {return}
我已經得到了一個庫下面的代碼:
private void addData(JSONObject jsonObject) {
if (jsonObject == null){
return;
}
if (jsonObject.has("elements")){
try {
addData(jsonObject.getJSONArray("elements"));
} catch (JSONException e) {
e.printStackTrace();
}
}else{
try {
getArray().put(jsonObject);
} catch (JSONException e) {
}
}
}
現在Android Studio中,我使用這個庫。當我進入它(通過按Ctrl + B去的函數的定義),我看到它,它反編譯這樣:
private void addData(JSONObject jsonObject) {
if(jsonObject != null) {
if(jsonObject.has("elements")) {
try {
this.addData(jsonObject.getJSONArray("elements"));
} catch (JSONException var3) {
var3.printStackTrace();
}
} else {
try {
this.getArray().put(jsonObject);
} catch (JSONException var4) {
;
}
}
}
}
我知道這有效它是相同的,但在我心中這會影響性能。我的代碼會更快,如果jsonObject
經常爲空,(因爲它不必跳到功能的末尾,然後跳出來,如第二個版本所做的那樣),如果jsonObject
是第二個版本,效率更高更經常地不是null
。
我對改變性能的聲明是否正確?Java是如何編譯它的呢?
編輯:
好了,因爲我看到一些答覆,指出這是完全一樣的,讓我再詳細一點。我有過C和C++的多門課程。這些語言會立即被翻譯成程序集,從而很容易看到所有的跳轉。
說,我們已經得到這個僞代碼:如果事情是假的
function a(){
//p1
b(); //jump to b()
//p2
}
function b(){
if (something){//if not, jump to. If, don't jump } (1st jump)
goto whereyoucamefrom;//whereyoucamefrom in this case is b(); in function a(), so it'll jump to p2 (1st jump)
}
goto whereyoucamefrom;//whereyoucamefrom is now also b();, so going to p2 again. (2nd jump, only when not something.)
}
這導致1周跳,如果事情是真實的,2:
function a(){
//p 1
b();
//p2
}
function b(){
if (something){
return;
}
}
這將被編譯成。這絕對看起來比我更有效:
function b(){
if (!something){//if not, jump to else (1st jump)
//body of function
goto afterelse;
}else{//if the body got executed, jump to } (1st jump)
goto endoffunction;
}
goto whereyoucamefrom;//this is endoffunction (2nd jump, always gets executed)
}
這總是會導致2跳。
編輯: 好吧,我將嘗試使用此編輯來澄清(再一次)我的意圖與這篇文章。
我的問題是有關跳轉(或跳轉)級別的優化。我的問題是,是不是真的
function a(){
if (something){
jump out of function;
}
//body
jump out of function;
}
比
function a(){
if (something){
jump over else
}else{
//body
}
jump out of function
}
更快?如果是這樣,爲什麼java編譯器不能正確執行此操作?請參閱原始帖子,瞭解我編寫的代碼和decompiled
版本的編譯版本。
似乎是一個很好的問題,誰下降了誰? – droidev
性能應該是一樣的。你必須檢查'jsonObject'至少一次以確定它是否爲'null'。反編譯的代碼在我的oppinion中稍微好一點,因爲它只將方法留在一個分支上(不需要'return;'需要)。 – Turing85
@ Turing85我知道你必須檢查是否爲null,但我也在考慮跳轉到這裏。當處理器執行時,跳轉(或跳轉)在處理器上非常昂貴,因爲它必須沖洗管道,至少需要3次,最多可達20個時鐘週期。 –