<meta name='google-adsense-platform-account' content='ca-host-pub-1556223355139109'/> <meta name='google-adsense-platform-domain' content='blogspot.com'/> <!-- data-ad-client=ca-pub-4320963827702032 --> <!-- --><style type="text/css">@import url(https://www.blogger.com/static/v1/v-css/navbar/3334278262-classic.css); div.b-mobile {display:none;} </style> </head><body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d7256432\x26blogName\x3dThe+Frustrated+Programmer\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLACK\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://frustratedprogrammer.blogspot.com/search\x26blogLocale\x3den_US\x26v\x3d2\x26homepageUrl\x3dhttp://frustratedprogrammer.blogspot.com/\x26vt\x3d4213664491834773269', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>
| Monday, July 19, 2004

Recently I found myself writing for loops that use Iterators a little differently. I used to write loops like this:

Iterator fooIter = bar.getFoos();
Foo foo;
while( fooIter.hasNext() ) {
foo = (Foo)fooIter.next();
[...]
}


And now I've moved to this:

for( Iterator fooIter = bar.getFoos().iterator(); fooIter.hasNext(); ) {
Foo foo = (Foo)fooIter.next();
[...]
}


Its nice and tight, but still readable. But I've always wondered about instantiating a temporary reference inside a loop. I wrote this code to try to tell what effect it has on performance.

I wrote this code:

public class Foo {
private static int SIZE = 999999;

public static void main( String args[] ) {
String strArray[] = new String[SIZE];
for( int i = 0; i < SIZE; i++ ) {
strArray[i] = Long.toString( System.currentTimeMillis() );
}

int count = 0;
long timer = System.currentTimeMillis();
for( int i = 0; i < SIZE; i++ ) {
String str = strArray[i];
count += str.length(); // make sure the loop does something;
}
System.out.println("timer: " + (System.currentTimeMillis() - timer) );

timer = System.currentTimeMillis();
String str;
for( int i = 0; i < SIZE; i++ ) {
str = strArray[i];
count += str.length(); // make sure the loop does something;
}
System.out.println("timer: " + (System.currentTimeMillis() - timer) );
System.out.println("count: " + count );
}
}


Well, the output wasn't too suprising, exactly the same times. I hope my test aren't completely bogus. I'd feel better if I had a definite confirmation of performance either way.

Until I can use Java 1.5 on a project I'll probably stick with this...