what is faster?

Discussions

Performance and scalability: what is faster?

  1. what is faster? (1 messages)

    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

    Threaded Messages (1)

  2. what is faster?[ Go to top ]

    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;
    }