Hi,
just a simple question:
which code will faster executed:
V1:
Java-Code:
public String getString() {
if (1==1) return "ok";
i(f2==2) return "ok";
if name.equals("name") return "ok";
return "not ok";
}
or V2:
Java-Code:
public String getString() {
String buf;
if (1==1) buf="ok";
i(f2==2) buf="ok";
if name.equals("name") buf="ok";
buf="not ok";
return buf;
}
I think that the compiler can optimize V1 so it can be faster executed by the way V1 is mostly not good readable.
thanks.
mfg Matthias
-
what is faster? (1 messages)
- Posted by: Matthias Koch
- Posted on: June 20 2005 08:25 EDT
Threaded Messages (1)
- what is faster? by Sualeh Hasan on June 22 2005 01:13 EDT
-
what is faster?[ Go to top ]
- Posted by: Sualeh Hasan
- Posted on: June 22 2005 01:13 EDT
- in response to Matthias Koch
V2 always return "not ok' irrespective of the condition v1 will be faster becuase compiler will replace the entire block by return "ok". here is the poosible complier output but it may vary
V1:
Java-Code:
public String getString()
{
return "ok";
}
for
V2:
Java-Code:
public String getString()
{
String buf = "ok";
buf = "ok";
if("name".equals("name"))
buf = "ok";
buf = "not ok";
return buf;
}