Haha yes, Mr. Dino's does work better at this level, but I can find more primes >:D

At any rate, here's the code that I used for my stopwatch:

public class StopWatch {
private long startTime = 0;
private long stopTime = 0;
private boolean running = false;
public long getElapsedTime() {
long elapsed;
if (running) elapsed = System.currentTimeMillis() - startTime;
else elapsed = stopTime - startTime;
return elapsed;
}
public long getElapsedTimeSecs() {
long elapsed;
if (running) elapsed = (System.currentTimeMillis() - startTime) / 1000;
else elapsed = (stopTime - startTime) / 1000;
return elapsed;
}
public void start() {
this.startTime = System.currentTimeMillis();
this.running = true;
}
public void stop() {
this.stopTime = System.currentTimeMillis();
this.running = false;
}
}

Just add that as another class to your project or whatever and create a stopwatch object, call stopwatch.start() to start it, and stop() to stop it, and getElapsedTime whatever to get it. This is the ending line I'm putting on my code to read the stats for code execution:

for(long element : primes)
System.out.println(element);
System.out.println("\nStats:");
System.out.println("The amount of time needed to execute was:");
System.out.println(stopwatch.getElapsedTime() + " milliseconds.");
System.out.println("Which is approximately " + stopwatch.getElapsedTimeSecs() + " seconds.");

As for how my code works:

public boolean isPrime(long num,long[] primes) {
int square=(int)Math.sqrt(num);
for(int i=0; primes[i++] <= square;)
if(num % primes[i]== 0)
return true;
return false;
}
public long[] returnPrimes(int num) {
long[] primes = new long[num];
primes[0] = 2;
primes[1] = 3;
primes[2] = 5;
long j = 5;
int inc = 4;
for(int i = 3; i < num; i++) {
for(j+= (inc = 6-inc); isPrime(j,primes); j+= (inc = 6-inc));
primes[i]=j;
}
return primes;
}

First I call the method returnPrimes() from my main method, with the amount of primes I want to generate (in our case 100000). It then creates an array that will hold all the primes. Since I know exactly how many primes I need to return, I can create a fixed-size array. Then I hardcode in the first three primes, 2,3, and 5 since they do not act like normal primes. Then I initialize "j" which will be the number that I am testing for primality. "inc" is the increment that I need to go up by. If you've ever noticed, primes go up by 2, then 4, then 2, then 4, then 6, etc. so go up by 2 every other incrementation, and 4 all the others, you will skip a lot of unnecessary numbers. I start the outside for loop which basically starts at the fourth element of the array (since I've already filled the first three) which is why i is initialized at three, then I keep going up until I have added all the elements. Since num will always be equal to primes.length, I use num since that won't require the computer to calculate it. My next line is the engine of my primeFinder which is amusing because it's an empty for loop. In this line, nothing is done inside the for loop and instead relies entirely on the conditional which makes the for loop exit:

This line basically increments j, while toggling inc between 2 and 4. So in fact, while inc is originally initialized to 4, it's almost immediately switched to 2. This is actually faster than initializing it to 2 and forcing the computer to check a conditional every time.

The for loop will only exit when j is prime, otherwise it'll just increment j. In fact, the isPrime method does not actually check to see if a number is prime. It checks to see if a number is NOT prime. It returns true if a number is not prime, and false if it is prime. Originally, I used

But inverting the booleans in the hardcode of isPrime and getting rid of the "!" is faster than using "!". And of course, the increment/statement executed every time just toggles inc again and brings j up by two or four depending on the inc. Because of the way the primeTesting for loop is constructed, I KNOW that when it exits, j will be prime, and since I initialized j outside the for loop, its value is saved after the for loop exits, thus, I just store j to the next open element in the array and do it again. One might notice how when the isPrime method is called, some values in the array might be zero. That's okay since the program will always exit before it hits those zero values because the square root of the number I'm checking for primality will always be less than that of the prime numbers already added inside the array. As Dino is doing, I'm only checking the number to test for divisibility against all the prime numbers below the square root of the number which means to check to see if 1299709 is prime, I only need to check for divisibility against 190 numbers. Then the program just returns the array after all values are filled. :)

By the way, I've actually already remodified this code and made the primes[] local to the class instead of to the method so I don't need to pass in the array every time :)

Hope that makes sense!