Why is the timer that starts first the slowest?

Viewed 44

Let me first explain what I am doing so that you can give me solutions more easily.

I have three configurations of the same database in localhost (MongoDB) with which my Java application interacts, and I should calculate the performance in milliseconds of the operations the Java application does with the three databases, to do this I am using Google's Stopwatch.

The problem is that if I sequentially calculate the timing of the operations, the timer that starts first is always the slowest. Why?

This is the code:

public void Find_By_Name(int number) {
        
        try {
            
            Generator generator = new Generator();
            Mongo mongo = new Mongo();
            Mongo_Export mongo_export = new Mongo_Export();
            Mongo_Export_2 mongo_export_2 = new Mongo_Export_2();
            Mongo_Export_3 mongo_export_3 = new Mongo_Export_3();
            
            generator.Generate_players(number);
            
            mongo.Drop_database("FootballStats");       //Drop Database_1. 
            mongo.Drop_database("FootballStats_2");     //Drop Database_2.  
            mongo.Drop_database("FootballStats_3");     //Drop Database_3. 
            
            ArrayList<Player> all_players = generator.getAll_players();
            ArrayList<Goalkeeper> all_goalkeepers = generator.getAll_goalkeepers();

            Stopwatch time = null;
            Stopwatch time_2 = null;
            Stopwatch time_3 = null;
            
            long ms = 0;
            long ms_2 = 0;
            long ms_3 = 0;
            
            mongo_export.Insert_players(all_goalkeepers, all_players);      // Insert documents in Database_1.
            mongo_export_2.Insert_players(all_goalkeepers, all_players);    // Insert documents in Database_2.
            mongo_export_3.Insert_players(all_goalkeepers, all_players);    // Insert documents in Database_3.
            
            /** Start timer Database configuration 1 */
            
            mongo_export.Find_by_name(all_goalkeepers, all_players);    //Search in the Database_1
            time.stop();
            /**----------------------------------------------------------------------*/
            
            /** Start timer Database configuration 2 */
            time_2 = Stopwatch.createStarted();
            mongo_export_2.Find_by_name(all_goalkeepers, all_players);  //Search in the Database_2
            time_2.stop();
            /**----------------------------------------------------------------------*/
            
            /** Start timer Database configuration 3 */
            time_3 = Stopwatch.createStarted();
            mongo_export_3.Find_by_name(all_goalkeepers, all_players);  //Search in the Database_3
            time_3.stop();  
            /**----------------------------------------------------------------------*/
            
            ms = time.elapsed(TimeUnit.MILLISECONDS);
            ms_2 = time_2.elapsed(TimeUnit.MILLISECONDS);
            ms_3 = time_3.elapsed(TimeUnit.MILLISECONDS);
            
            System.out.println("Timer for Database_1: " + ms + " ms.");
            System.out.println("Timer for Database_2:: " + ms_2 + " ms.");
            System.out.println("Timer for Database_3:: " + ms_3 + " ms.");
            
        } catch (Exception e) {
            
            System.out.println("Error in  Find_By_Name().");
            
        }
        
    }

If, for example, I start the timer for database 2 first, this will be the one with the highest value compared to the database timer 1 and 3. And the same is true if I start the database timer 3 first.

I also tried not to evaluate sequentially but to execute the function once at a time for each database, and in this way all the databases are at the same conditions and with the same delay delta, but by doing so in the 3 databases they are not inserted the same documents, because they are generated randomly at each execution of any function, and therefore the documents have different sizes. I would like all databases to have the same documents as well as the same number of documents.

I don't want to eliminate the "delay", but I would like all databases to be evaluated on equal terms. Any solution?

0 Answers
Related