cpu cycles to create a variable

?

-

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.
 
E

Edwin Martin

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
 
T

Tony Morris

- said:
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
 
R

R.F. Pels

- said:
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.
 
T

tzvika.barenholz

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.
 
P

Patricia Shanahan

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
 
L

Lisa

- said:
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.
 
K

Kevin McMurtrie

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top