0

First of all, this question is quite generic, I fully recognise that. I cannot share the code of the text parsers as I'm working on some private data, but the context is as follows. I've been coding for about a year in Java (so my apologies if this seems uninformed). I wrote a text parser that dealt with pulling data from a website, and my original one was quite inefficent (like O(11n) if you want it asymptotically) which I know is still linear but the text file was huge, and the inefficiency shows by the fact that I got it down to O(n), essentially pulling everything I want in one go rather than 11 different gos.

To test if this efficiency improvement had made any empirical difference, I created a tester class that would start a stopwatch, run the first parser, print the time, end the first stopwatch, start a second one, run the second parser, and print the time. When I run the parsers separately, the first one is approximately 1.5-3 seconds (volatile, I know), and the second one is almost always 0.7 secs. When I run them through that independent tester class, the first one is slightly faster (1.5-2.5) but the second one runs absurdly quicker at around 0.05-0.07 seconds.

Is there any reason for this? I can't put my finger on it. Sorry if this is a generic question, it's not life or death but it's really intrigued me. For what it's worth, both parsers work perfectly/have the exact same outcome whether they're run by themselves or through the tester class.

If it helps, here's the code for the tester class. Sorry I can't share the code for the parsers.

public class ParserSpeedTest {
  public static void main(final String[] args) {

  Stopwatch st1 = new Stopwatch();
  className.runParser1();
  double time1 = st1.elapsedTime();
  System.out.println("Elapsed Time: " + time1+" s");

  System.out.println("---------------------------");

  Stopwatch st2 = new Stopwatch();
  otherClassName.runParser2();
  double time2 = st2.elapsedTime();
  System.out.println("Elapsed Time: " + time2+" s");
  }
}
fourteen14
  • 75
  • 7
  • 1
    I have no experience with Java but I have always heard that Java byte-code needs a "warm-up" to get JIT-compiled. Maybe your two parsers rely on the same internal resources (classes), and if you permute the invocation of these two parsers (or invoke only one parser twice) you will notice the same speedup on the second invocation. Or maybe the huge amount of data to parse is already loaded in memory at the second invocation... – prog-fh Jul 01 '20 at 11:53
  • 1
    https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java - For a start you should run the parsers multiple times. Interchange which parser you run - running parser1 10 times first and just then parser2 isn't a good option. – Amongalen Jul 01 '20 at 12:07
  • prog-fh and Amongalen - you guys are both spot on. I switched them around and got about 0.7/0.8 on both parsers. Can't believe I didn't think to switch them around, I had run them a load of times going 1-2 but never switched to 2-1, seems like an obvious idea in hindsight. Thanks both, and thank you for the technical explanation prog. – fourteen14 Jul 01 '20 at 12:34

0 Answers0