tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Leon Rosenberg" <rosenberg.l...@googlemail.com>
Subject Re: Code performance question #1
Date Mon, 07 Aug 2006 15:58:26 GMT
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; i<data.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 <dckerber@verizon.net> 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: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

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


Mime
View raw message