Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > cpu cycles to create a variable

Reply
Thread Tools

cpu cycles to create a variable

 
 
-
Guest
Posts: n/a
 
      03-20-2005
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.
 
Reply With Quote
 
 
 
 
Edwin Martin
Guest
Posts: n/a
 
      03-20-2005
> 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/
 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      03-20-2005
"-" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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/



 
Reply With Quote
 
R.F. Pels
Guest
Posts: n/a
 
      03-20-2005
- 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
 
Reply With Quote
 
tzvika.barenholz@gmail.com
Guest
Posts: n/a
 
      03-20-2005
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.


 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-20-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Lisa
Guest
Posts: n/a
 
      03-20-2005

"-" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Kevin McMurtrie
Guest
Posts: n/a
 
      03-22-2005
In article <(E-Mail Removed)>, - <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Applets, Seperate Frames, and CPU cycles/Efficiency Russ Java 4 05-02-2005 11:07 PM
How many CPU cycles does an instruction take ? Kevin Klein C++ 4 04-02-2004 03:32 PM
How many CPU cycles does an instruction take ? Kevin Klein C Programming 4 04-02-2004 03:32 PM
Re: How many CPU cycles does an instruction take ? Kevin Klein C++ 2 04-02-2004 03:23 AM
Re: How many CPU cycles does an instruction take ? Kevin Klein C Programming 2 04-02-2004 03:23 AM



Advertisments