Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:11:34+00:00 2026-06-17T22:11:34+00:00

Using a few different methods (binary search, nested loops etc.), I’m measuring the running

  • 0

Using a few different methods (binary search, nested loops etc.), I’m measuring the running time on average of finding common elements between two arrays. However, when I increase the # of time it loops (I divide runtime by number of loops at the end), the average runtime per iteration decreases. That is, it gets significantly faster.

I discovered the line in my main method that causes this, so I won’t post the rest of main:

int intersection=studentList.intersectionSizeNestedLoops(firstList1,secondList1);

I looked at the runtime for each iteration instead of just the average at the end, and I noticed why exactly. For some reason, the first few iterations always take much longer (depends on list size, but e.g. 500 000 ns for the first few vs 20 000 ns for everything after, so it averages out to a lower run time when you have more iterations and thus more of the 20 000 ns iterations).

There’s no way it’s any of the algorithms though, so it must be the studentlist method.

Generates lists:

public studentList(int size, String course) {
int IDrange = 2*size;
studentID = new int[size];
boolean[] usedID = new boolean[IDrange];

for (int i = 0; i < IDrange; i++) 
        usedID[i] = false;

for (int i=0;i<size;i++) {
    int t;

    do {
        t = (int) (Math.random()*IDrange);
    } while (usedID[t]);

    usedID[t] = true;
    studentID[i] = t;
}

courseName = course;
numberOfStudents = size;
}

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:11:35+00:00Added an answer on June 17, 2026 at 10:11 pm

    This is perfectly normal. The program gets faster because the just-in-time compiler kicks in and compiles parts of the code.

    You can verify this by starting the JVM with -XX:+PrintCompilation.

    Timing Java code can be tricky. See How do I write a correct micro-benchmark in Java? for a good discussion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a head file which I am using for a few different pages.
I am using Hudson as a CI server to build a few different .Net
I'm using NSCoding to manage a custom object with a few different fields, one
Here is a C# program that tries Marshal.SizeOf on a few different types: using
I've worked at few places and seen two different methods of doing a section
I have a few questions about Java multi-threading. I am currently learning different methods
I have seen a few different methods for adding classes to dynamically created elements
i've tried a few different json methods (stringify, toJSON, and probably some totally irrelevant
Possible Duplicate: Ambiguous reference in WCF and client application I am using few classes
After using a few validation libraries I finally decided to roll my own implementation,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.