Java Reference
In-Depth Information
Listing 13-3. A Script with a Nashorn Callsite Profile Directive Enabled for a Function
// primeprofiler.js
function isPrime(n) {
// Profile this function only
"nashorn callsite profile";
// Integers <= 2, floating-point numbers, and even numbers are not primes
if (n <= 2 || Math.floor(n) !== n || n % 2 === 0) {
return false;
}
// Check if n is divisible by any odd integers between 3 and sqrt(n).
var sqrt = Math.sqrt(n);
for (var i = 3; i <= sqrt; i += 2) {
if (n % i === 0) {
return false;
}
}
return true; // If we get here, it is a prime number.
}
// Check few nubmers for being primes
var num = 8;
var isPrimeNum = isPrime(num);
print(num + " is a prime number: " + isPrimeNum);
num = 37;
isPrimeNum = isPrime(num);
print(num + " is a prime number: " + isPrimeNum);
The following command runs the script in the primeprofile.js file with Nashorn
debug option enabled:
c:\>jjs -J-Dnashorn.debug=true primeprofile.js
8 is a prime number: false
37 is a prime number: true
C:\
The command will generate a file named NashornProfile.txt in the current directly
that contains the profile data for the isPrime() function call. The contents of this file are
shown in Listing 13-4.
Search WWH ::




Custom Search