Creating objects makes the VM faster?

Posted by Sudhir Jonathan on Stack Overflow See other posts from Stack Overflow or by Sudhir Jonathan
Published on 2009-09-27T19:13:50Z Indexed on 2010/03/12 17:47 UTC
Read the original article Hit count: 213

Look at this piece of code:

MessageParser parser = new MessageParser();
for (int i = 0; i < 10000; i++) {
    parser.parse(plainMessage, user);
}

For some reason, it runs SLOWER (by about 100ms) than

for (int i = 0; i < 10000; i++) {
    MessageParser parser = new MessageParser();
    parser.parse(plainMessage, user);
}

Any ideas why? The tests were repeated a lot of times, so it wasn't just random. How could creating an object 10000 times be faster than creating it once?

© Stack Overflow or respective owner

Related posts about java

Related posts about optimization