Code performance question #1

2006-08-07 Thread David Kerber
I have a couple of questions about the performance of my code, but I'm 
going to ask them in separate threads.


The first one is, if I have this loop:

   for ( ii = 0; ii  data.length; ii++ ) {

where data is defined as byte[] , is the .length property evaluated each 
time through the loop, or is it only evaluated once?  I know many 
languages only evaluate it once, so there's no performance benefit to 
storing it in an integer and using that as the loop upper index, but 
don't know if that is also the case for Java.  The data length usually 
runs about 50 bytes, and this loop is executed more than 2 million times 
per day, so even a small performance improvement is helpful.


Thanks for any info!
Dave



-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Code performance question #1

2006-08-07 Thread David Kerber

By the way, this code is in a servlet running under 5.5.12, if it matters.

David Kerber wrote:

I have a couple of questions about the performance of my code, but I'm 
going to ask them in separate threads.


The first one is, if I have this loop:

   for ( ii = 0; ii  data.length; ii++ ) {

where data is defined as byte[] , is the .length property evaluated 
each time through the loop, or is it only evaluated once?  I know many 
languages only evaluate it once, so there's no performance benefit to 
storing it in an integer and using that as the loop upper index, but 
don't know if that is also the case for Java.  The data length usually 
runs about 50 bytes, and this loop is executed more than 2 million 
times per day, so even a small performance improvement is helpful.


Thanks for any info!
Dave



-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]







-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Code performance question #1

2006-08-07 Thread Leon Rosenberg

data.length is evaluated each time.

here's the example to demonstrate it:

public class TestLoop {
public static void main(String a[]){
byte data[] = new byte[10];
int counter = 0;
for (int i=0; idata.length; i++){
if (i==5)
data = new byte[20];
counter++;
}

System.out.println(counter);
}
}

if data.length would be evaluated once the output would be 10. But if
you run this program it prints out 20. So the data.length field is
evaluated each time.

regards
Leon

P.S. 2.000.000 times per day * 50 = 100.000.000 operations per day
= 10.000.000 operations per hour
= 166.667 operation per minute
= 2.777 operations per second.

Given 100.000.000 operations per second your processor can manage, the
performance benefit would be zero.




On 8/7/06, David Kerber [EMAIL PROTECTED] wrote:

I have a couple of questions about the performance of my code, but I'm
going to ask them in separate threads.

The first one is, if I have this loop:

for ( ii = 0; ii  data.length; ii++ ) {

where data is defined as byte[] , is the .length property evaluated each
time through the loop, or is it only evaluated once?  I know many
languages only evaluate it once, so there's no performance benefit to
storing it in an integer and using that as the loop upper index, but
don't know if that is also the case for Java.  The data length usually
runs about 50 bytes, and this loop is executed more than 2 million times
per day, so even a small performance improvement is helpful.

Thanks for any info!
Dave



-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Code performance question #1

2006-08-07 Thread David Kerber

Thanks for the help!


Leon Rosenberg wrote:


data.length is evaluated each time.

here's the example to demonstrate it:

public class TestLoop {
public static void main(String a[]){
byte data[] = new byte[10];
int counter = 0;
for (int i=0; idata.length; i++){
if (i==5)
data = new byte[20];
counter++;
}
   
System.out.println(counter);

}
}

if data.length would be evaluated once the output would be 10. But if
you run this program it prints out 20. So the data.length field is
evaluated each time.

regards
Leon

P.S. 2.000.000 times per day * 50 = 100.000.000 operations per day
= 10.000.000 operations per hour
= 166.667 operation per minute
= 2.777 operations per second.

Given 100.000.000 operations per second your processor can manage, the
performance benefit would be zero.




On 8/7/06, David Kerber [EMAIL PROTECTED] wrote:


I have a couple of questions about the performance of my code, but I'm
going to ask them in separate threads.

The first one is, if I have this loop:

for ( ii = 0; ii  data.length; ii++ ) {

where data is defined as byte[] , is the .length property evaluated each
time through the loop, or is it only evaluated once?  I know many
languages only evaluate it once, so there's no performance benefit to
storing it in an integer and using that as the loop upper index, but
don't know if that is also the case for Java.  The data length usually
runs about 50 bytes, and this loop is executed more than 2 million times
per day, so even a small performance improvement is helpful.

Thanks for any info!
Dave






-
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]