cpu cycles to create a variable

Discussion in 'Java' started by -, Mar 20, 2005.

  1. -

    - Guest

    which is faster where x and y are small numbers that don't take too long
    to count.

    if (string.length() >= x && string.length() < y) {
    ...
    }

    or

    int stringLength = string.length();
    if (stringLength >= x && stringLength < y) {
    ...
    }


    and how many cpu 'cycles' does it take to initialize a variable so that
    it can be determined which of the two methods above should be used
    depending on the values of x and y.

    i mean if x is 0 and y is 9, perhaps method 1 is recommended but if x is
    0 and y is some big number, then it is better off to use method 2.
     
    -, Mar 20, 2005
    #1
    1. Advertising

  2. -

    Edwin Martin Guest

    > which is faster where x and y are small numbers that don't take too long
    > to count.
    >
    > if (string.length() >= x && string.length() < y) {
    > ...
    > }
    >
    > or
    >
    > int stringLength = string.length();
    > if (stringLength >= x && stringLength < y) {
    > ...
    > }
    >
    >
    > and how many cpu 'cycles' does it take to initialize a variable so that
    > it can be determined which of the two methods above should be used
    > depending on the values of x and y.
    >
    > i mean if x is 0 and y is 9, perhaps method 1 is recommended but if x is
    > 0 and y is some big number, then it is better off to use method 2.


    Java does not count the length of a string the way C does.

    Java holds the length in a member variable, so getting the length of a
    string is very fast, no matter how long the string is.

    Making a method call has a little bit overhead and creating a variable
    has a little overhead, zo I can't tell which of your examples is the
    fastest.

    Because String is a final class, the compiler might even optimize the
    code for you so it might not be a problem anyway.

    Second, how many times is the code executed? Millions of times?
    Otherwise is doesn't really make sense to optimize code which is only
    executed a couple of times. Then it is far more important to write
    readable code, code which looks "comfortable".

    Edwin Martin



    --
    http://www.bitstorm.org/edwin/en/
     
    Edwin Martin, Mar 20, 2005
    #2
    1. Advertising

  3. -

    Tony Morris Guest

    "-" <> wrote in message news:...
    > which is faster where x and y are small numbers that don't take too long
    > to count.
    >
    > if (string.length() >= x && string.length() < y) {
    > ...
    > }
    >
    > or
    >
    > int stringLength = string.length();
    > if (stringLength >= x && stringLength < y) {
    > ...
    > }
    >
    >
    > and how many cpu 'cycles' does it take to initialize a variable so that
    > it can be determined which of the two methods above should be used
    > depending on the values of x and y.
    >
    > i mean if x is 0 and y is 9, perhaps method 1 is recommended but if x is
    > 0 and y is some big number, then it is better off to use method 2.


    To even suggest that there is a concrete answer to your question, or for
    anyone to provide a perceived correct answer, only undermines the
    complexities involved in "just-in-time" compiling. And even so, "design then
    optimise".

    http://www.google.com/search?q=jit compilers&hl=en
    http://c2.com/cgi/wiki?PrematureOptimization

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Mar 20, 2005
    #3
  4. -

    R.F. Pels Guest

    - wrote:

    > if (string.length() >= x && string.length() < y) {
    > ...
    > }
    >
    > or
    >
    > int stringLength = string.length();
    > if (stringLength >= x && stringLength < y) {
    > ...
    > }


    Rule #1: Do not optimize
    Rule #2: Do not optimize. Yet.

    In other words: MU, or, the question should be unasked. I and many others
    are of the opinion that hand optimization - as in the second code sample -
    has far worse effects than the slight possibility that the first might be
    or might not be a little bit more inefficient.

    And for that matter, it is quite possible that compilers do invariant code
    motion in similar cases, automatically creating the equivalent of the
    second form without bothering you.

    --
    Ruurd
    ..o.
    ...o
    ooo
     
    R.F. Pels, Mar 20, 2005
    #4
  5. -

    Guest

    Everything everyone said about this not being a scenario where you
    should optimize is true. For optimization there are profilers etc.

    Having said that I couldn't help but make the observation that
    whichever method is faster, it has absolutely nothing to do with the
    values of x and y does it? that ALU operation for > takes exactly the
    same for a big int that it does for a little one.


    - wrote:
    > which is faster where x and y are small numbers that don't take too

    long
    > to count.
    >
    > if (string.length() >= x && string.length() < y) {
    > ...
    > }
    >
    > or
    >
    > int stringLength = string.length();
    > if (stringLength >= x && stringLength < y) {
    > ...
    > }
    >
    >
    > and how many cpu 'cycles' does it take to initialize a variable so

    that
    > it can be determined which of the two methods above should be used
    > depending on the values of x and y.
    >
    > i mean if x is 0 and y is 9, perhaps method 1 is recommended but if x

    is
    > 0 and y is some big number, then it is better off to use method 2.
     
    , Mar 20, 2005
    #5
  6. wrote:

    > Everything everyone said about this not being a scenario where you
    > should optimize is true. For optimization there are profilers etc.


    I agree.

    > Having said that I couldn't help but make the observation that
    > whichever method is faster, it has absolutely nothing to do with the
    > values of x and y does it? that ALU operation for > takes exactly the
    > same for a big int that it does for a little one.


    In some cases a narrow literal can be embedded in an
    instruction, but a wider literal is loaded into a register,
    so the loop with the wider literal might have more instructions.

    However, on modern processors, in most loops the integer
    arithmetic is free. The critical path is the combination of
    jumps and memory access. Don't think of the processor as a
    single entity doing one thing at a time. It is a number of
    units, working collaboratively to get through the code as
    fast as they can. Really easy things, like integer
    comparison, get hidded away behind time consuming tasks such
    as cache misses.

    Patricia
     
    Patricia Shanahan, Mar 20, 2005
    #6
  7. -

    Lisa Guest

    "-" <> wrote in message news:...
    > which is faster where x and y are small numbers that don't take too long
    > to count.
    >
    > if (string.length() >= x && string.length() < y) {
    > ...
    > }
    >
    > or
    >
    > int stringLength = string.length();
    > if (stringLength >= x && stringLength < y) {
    > ...
    > }
    >
    >
    > and how many cpu 'cycles' does it take to initialize a variable so that
    > it can be determined which of the two methods above should be used
    > depending on the values of x and y.
    >
    > i mean if x is 0 and y is 9, perhaps method 1 is recommended but if x is
    > 0 and y is some big number, then it is better off to use method 2.


    When I was a little kid, compilers and such were not so good and you
    had to worry about this sort of thing. Today, however, you should write
    the code to be as reliable and as understandable as possible and assume
    that 50 years of evolution has solved these petty little things. So I would
    use the first one. The second one has an extra thing that you have to
    test etc. and they will probably run at the same speed.
     
    Lisa, Mar 20, 2005
    #7
  8. In article <>, - <> wrote:

    > which is faster where x and y are small numbers that don't take too long
    > to count.
    >
    > if (string.length() >= x && string.length() < y) {
    > ...
    > }
    >
    > or
    >
    > int stringLength = string.length();
    > if (stringLength >= x && stringLength < y) {
    > ...
    > }
    >
    >
    > and how many cpu 'cycles' does it take to initialize a variable so that
    > it can be determined which of the two methods above should be used
    > depending on the values of x and y.
    >
    > i mean if x is 0 and y is 9, perhaps method 1 is recommended but if x is
    > 0 and y is some big number, then it is better off to use method 2.



    It's pretty much impossible to count the cost of a variable. Usually
    creating one has no cost. There is some cost to keeping it in use
    because it may force other variables out of fast access locations, like
    a register. Your second example doesn't keep the temporary int in use
    so its space will be reclaimed immediately.

    Which of the above is faster? Depends on the native compiler. The
    compiler may realize that the variable behind String.length() is
    logically immutable and transform the first example to the second. It
    should at least notice that String is declared final and inline the
    method's code to create direct access to the contents of the String.
    Anything is possible once past the bytecode verifier.

    Benchmark it and let us know.
     
    Kevin McMurtrie, Mar 22, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ
    Replies:
    4
    Views:
    432
    Piotr Gaertig /Gertas/
    May 3, 2005
  2. Kevin Klein
    Replies:
    4
    Views:
    671
    Dan Pop
    Apr 2, 2004
  3. Kevin Klein
    Replies:
    2
    Views:
    928
    those who know me have no need of my name
    Apr 2, 2004
  4. Kevin Klein

    How many CPU cycles does an instruction take ?

    Kevin Klein, Apr 1, 2004, in forum: C Programming
    Replies:
    4
    Views:
    1,263
    Dan Pop
    Apr 2, 2004
  5. Kevin Klein

    Re: How many CPU cycles does an instruction take ?

    Kevin Klein, Apr 1, 2004, in forum: C Programming
    Replies:
    2
    Views:
    775
    those who know me have no need of my name
    Apr 2, 2004
Loading...

Share This Page