We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Check these:
https://forum.processing.org/two/search?Search=nanosecond
https://forum.processing.org/two/discussion/19731/error-waited-5000ms#latest
and check this other post as it could be relevant to your case:
Kf
That's the same thing, millis or nanosecond ^^
you can maybe use millis() or access the underlying java millisecond and "nanosecond" timers.
http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime
if you set the framerate as high as possible then it'll effectively disable the 60fps 'ticks' that are the default.
"Or that's too tough for ya to figure out?"
Bad code isn't necessarily non-working code. It is code we have to spend time on it to understand its intent. Condensing lines to get shorter code isn't considered a good practice, even less for code given to newbies. Today, good code is readable code, easy to maintain and to extend, not code where you saved (potentially) a nanosecond or two out of a 100 ns execution time...
When I discovered C (after the verbose Pascal), I was glad to use tricks like balls[j = i++] as it seemed clever. Today, I just make two lines (or more) out of it.
Note: when you write int j, i = 0;
, j is uninitialized in Java. Idem for p in Bubble p, b = ...
Not sure if you tested your code in the PDE.
In order to properly warm-up, we gotta call the actual methods being tested! :-w
isPrime(62131125996267401L);
isPrimeJS(62131125996267401L);
isPrime(46331401803043001L);
isPrimeJS(46331401803043001L);
isPrime(38924154034357817L);
isPrimeJS(38924154034357817L);
I've replaced both isPrime() & isPrimeJS() w/ my own versions.
I've only had to modify them to use a long
iterator since 9051643576320000001L
is a 63-bit value!
If that was at most 62-bit, my unmodified int
versions coulda been used instead! :-<
Probably would net slightly better results methinks...
Indeed, isPrimeJS() is the clear winner. W/ my version slightly faster than the 1 you tested with! \m/
And my own modified isPrimeLong(), using 6
step iterator jumps is much faster than the 1 using 2
only!
P.S.: My isPrimeJS() when using |
rather than ||
in the chained check expression is a few millis faster! :P
However, it's a tiny slower for the isPrimeLong()'s case! #-o
double test1, test2;
void setup() {
println("Warming up the CPU...");
warmup(10000000); //Let's get that CPU warmed up, so all comparisons are equal.
isPrimeLong(62131125996267401L);
isPrimeJS(62131125996267401L);
isPrimeLong(46331401803043001L);
isPrimeJS(46331401803043001L);
isPrimeLong(38924154034357817L);
isPrimeJS(38924154034357817L);
println("Initializing nanosecond timer...\n");
println("Done. Prime being tested is 9051643576320000001 (19 digits long).");
println("The prime number is fairly large, so be patient as the test occurs.\n");
Stopwatch s = new Stopwatch();
println("First algorthim. The original code, isPrime().");
println("\nStart.");
s.start();
println(isPrimeLong(9051643576320000001L));
s.stop();
println("Test complete. Time taken: " + s.getNanoTime() / 1000000d + " ms.\n");
test1 = s.getNanoTime() / 1000000d;
println("Second algorthim. Modified from JavaScripter, isPrimeJS().");
println("\nStart.");
s.start();
println(isPrimeJS(9051643576320000001L));
s.stop();
println("Test complete. Time taken: " + s.getNanoTime() / 1000000d + " ms.");
test2 = s.getNanoTime() / 1000000d;
boolean test1Wins = test1 < test2;
String winner = (test1Wins ? "IsPrime()" : "IsPrimeJS()");
String runnerUp = (test1Wins ? "IsPrimeJS()." : "IsPrime().");
double time = (test1Wins ? test1 : test2);
double ratio = (test1Wins ? test2 / test1 : test1 / test2);
println("Winner: " + winner + ", with a time of: " + time
+ " milliseconds, which is " + String.format("%.5f", ratio)
+ " faster than " + runnerUp);
exit();
}
void warmup(int upperLimit) {
int[] primes = new int[(upperLimit + 1) / 2];
int index = 1;
primes[0] = 2;
loop:
for (int i = 3; i <= upperLimit; i += 2) {
for (int p : primes) {
if (p * p > i) break;
if (i % p == 0) continue loop;
}
primes[index] = i;
index++;
}
}
//The original code
static final boolean isPrimeLong(long n) {
if (n <= 3L) return n >= 2L;
if ((n&1L) == 0L | n%3L == 0L) return false;
long i = -1L, sqrtN = (long)Math.sqrt(n) + 1L;
while ((i+=6L) <= sqrtN)
if (n%i == 0L || n%(i+2L) == 0L) return false;
return true;
}
//JavaScripter's code
static final boolean isPrimeJS(long n) {
if (n <= 5L) return n >= 2L & n != 4L;
if ((n&1L) == 0L | n%3L == 0L | n%5L == 0L) return false;
long i = -23L, sqrtN = (long)Math.sqrt(n) + 1L;
while ((i+=30L) <= sqrtN) if
( n%i == 0L
| n%(i+4L) == 0L
| n%(i+6L) == 0L
| n%(i+10L) == 0L
| n%(i+12L) == 0L
| n%(i+16L) == 0L
| n%(i+22L) == 0L
| n%(i+24L) == 0L) return false;
return true;
}
class Stopwatch {
private long startTime;
private long stopTime;
public void start() {
startTime = System.nanoTime();
stopTime = startTime;
}
public void stop() {
stopTime = System.nanoTime();
}
public long getNanoTime() {
return stopTime - startTime;
}
}
Well, here's a bit of a test. An even match between the 2 algorithms.
Most of the the algorithms, excluding the one from JavaScripter, were for the most part the same, so I just took the most optimized one (feel free to replace it if you have a better one).
Here's the test code. I have a relatively high end computer, now, so isPrime() took 13.5 seconds to run, whereas isPrimeJS() to 6.7 seconds.
double test1, test2;
void setup() {
println("Warming up the CPU...");
warmup(10000000); //Let's get that CPU warmed up, so all comparisons are equal.
println("Initializing nanosecond timer...\n");
println("Done. Prime being tested is 9051643576320000001 (19 digits long).");
println("The prime number is fairly large, so be patient as the test occurs.\n");
Stopwatch s = new Stopwatch();
println("First algorthim. The original code, isPrime().");
println("\nStart.");
s.start();
println(isPrime(9051643576320000001L));
s.stop();
println("Test complete. Time taken: " + s.getNanoTime() / 1000000d + " ms.\n");
test1 = s.getNanoTime() / 1000000d;
println("Second algorthim. Modified from JavaScripter, isPrimeJS().");
println("\nStart.");
s.start();
println(isPrimeJS(9051643576320000001L));
s.stop();
println("Test complete. Time taken: " + s.getNanoTime() / 1000000d + " ms.");
test2 = s.getNanoTime() / 1000000d;
boolean test1Wins = test1 < test2;
String winner = (test1Wins ? "IsPrime()" : "IsPrimeJS()");
String runnerUp = (test1Wins ? "IsPrimeJS()." : "IsPrime().");
double time = (test1Wins ? test1 : test2);
double ratio = (test1Wins ? test2 / test1 : test1 / test2);
println("Winner: " + winner + ", with a time of: " + time + " milliseconds, which is " + String.format("%.5f", ratio) + " faster than " + runnerUp);
}
void warmup(int upperLimit) { //Sieve of Eratosthenes, used as a moderately computation intensive warm up.
int[] primes = new int[(upperLimit + 1) / 2];
int index = 1;
primes[0] = 2;
loop:
for (int i = 3; i <= upperLimit; i += 2) {
for (int p : primes) {
if (p * p > i) break;
if (i % p == 0) continue loop;
}
primes[index] = i;
index++;
}
}
//The original code
public static boolean isPrime(long num) {
if (num == 2) return true;
if ((num & 1l) == 0) return false;
for(long i = 3; i * i < num; i += 2) if(num % i == 0) return false;
return true;
}
//JavaScripter's code
public static boolean isPrimeJS(long num) {
if (num % 2 == 0) return false;
if (num % 3 == 0) return false;
if (num % 5 == 0) return false;
for (long i = 7; i * i <= num; i += 30) {
if (num % i == 0) return false;
if (num % (i + 4) == 0) return false;
if (num % (i + 6) == 0) return false;
if (num % (i + 10) == 0) return false;
if (num % (i + 12) == 0) return false;
if (num % (i + 16) == 0) return false;
if (num % (i + 22) == 0) return false;
if (num % (i + 24) == 0) return false;
}
return true;
}
class Stopwatch {
private long startTime;
private long stopTime;
public void start() {
startTime = System.nanoTime();
stopTime = startTime;
}
public void stop() {
stopTime = System.nanoTime();
}
public long getNanoTime() {
return stopTime - startTime;
}
}
The Stopwatch class is there, because it times the algorithms in nanoseconds, rather than milliseconds. This helps get a more accurate reading.
Tell us what you get.