I wrote a small class that basically retrieves data from a CSV file and loads it into a POJO object. Since I need frequent access to this data I wrote a singleton class that checks if the data is already in the object and if yes, it simply returns the data directly from object (without needing to get it from file again). Otherwise, it retrieves data from file and stores data in object for future queries.
When testing I noticed it takes roughly 175 milliseconds to access the data 10,000 times (inclduing the first time which loads the data from file).
What struck me was that when I looped 20,000 times it took only 177 milliseconds (only two milliseconds more than 10,000 times) and 50,000 times took only about 197 milliseconds.
What is the explanation that it is so much quicker to do 50K vs. 10K? Why doesn't the time increase proportionally?
Also why is accessing data directly from object so much faster than accessing it from disk (when I access it via file it takes about 160 milliseconds for a single time)
Thanks
Update:
Perhaps even more perplexing is that when I attempt to access the object using two different keys (which requires two reads from file) it takes roughly the exact same amount of time (with a 1 millisecond variation) than accessing it once. All of the explanations regarding object access being 200K times faster than file access explains only my first observation but now I'm actually reading data from two different files yet I don't see a proportional increase in the amount of time it takes.
In other words, doing this:
for (int counter = 0; counter < 1; counter++) {
POJOObj.getInstance().getKey("Key1", "Val1");
}
takes the same amount of time as doing this:
for (int counter = 0; counter < 1; counter++) {
POJOObj.getInstance().getKey("Key1", "Val1");
POJOObj.getInstance().getKey("Key1", "Val2"); // this requires new read from file
}
Why does the time not increase proportionally?