Cast not your Perl before swine?

Dave Smith dave at thesmithfam.org
Tue Dec 18 10:28:01 MST 2007


Bryan Sant wrote:
> <JAVA CODE>
> public class CreateObject {
>   public static void main(String args[]) {
>     long start = System.currentTimeMillis();
>
>     for (int n = 0; n < 10 * 1000 * 1000; n++) {
>       new CreateObject(); new CreateObject();
>       new CreateObject(); new CreateObject();
>       new CreateObject(); new CreateObject();
>       new CreateObject(); new CreateObject();
>       new CreateObject(); new CreateObject();
>     }
>
>     long end = System.currentTimeMillis();
>     System.out.println((end - start) / 1000.0 + " seconds");
>   }
> }
> </JAVA CODE>
>   

Is this a case of the Java interpreter noticing that the contents of the 
loop have no effect, and so it skips it? Does it know because class 
CreateObject has no members, and no non-static methods?

In the mean time, I'll just assume that Java is capable of allocating 
and freeing 100 million objects in under 1 second. :)

If you added a line of code in the body of the loop that actually *does* 
something (like store a reference to one of the new CreateObject 
instances, where the reference is declared outside the loop), I imagine 
this code would take as long as the C version, eh?

--Dave



More information about the PLUG mailing list