SlideShare a Scribd company logo
Object Comparisons
Finding Prime Numbers
C++ & Java
ADAM FELDSCHER
I699 PERFORMANCE DESIGN PATTERNS II
10/16/2019
Modifications
 All tests are done for primes up to 10k, with 40 iterations
 1k had too much noise
 Java and C++
 Java Primitives vs Wrappers
 PrimeFinder Class Invoction
 New instance per each iteration
 Bring SQRT into for loop comparison
 Adding is_prime method
Algorithm (Java)
for (int iter = 0; iter < max_iter; iter++) {
// insert delays
long start_time = System.nanoTime();
for (int n = 2; n <= max_prime; n++) {// 2 - 10k
int max_divisor = (int) sqrt(n);
boolean is_prime = true;
for (int i = 2; i <= max_divisor; i++) {// check for divisors
if (n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
num_primes_found++; // count primes
}
}
long end_time = System.nanoTime();
// print things
num_primes_found = 0;
}
Java Consistency Check
0
1000000
2000000
3000000
4000000
5000000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Nanoseconds
Iteration
Java 10, 10k, normal consistency
Run 1 Run 2
Java Primitives vs Wrappers
Java Primitives vs Wrappers
(Integer)
0
5000000
10000000
15000000
20000000
25000000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Nanoseconds
Iteration
Java 10, 10k, Integer
Primative Integer
Java PrimeFinder Class
public class PrimeFinder {
private Integer max_prime;
public PrimeFinder(Integer max_prime) {
this.max_prime = max_prime;
}
public Integer find_primes() {
Integer num_primes_found = 0;
for (Integer n = 2; n <= max_prime; n++) {
Integer max_divisor = (int) sqrt(n);
Boolean is_prime = true;
for (Integer i = 2; i <= max_divisor; i++) {
if (n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
num_primes_found++;
}
}
return num_primes_found;
}
}
public static void main(String[] args) {
for (Integer iter = 0; iter < max_iter; iter++) {
//Start Timer
PrimeFinder primeFinder = new PrimeFinder(iter, max_prime);
num_primes_found = primeFinder.find_primes();
//End timer & Print
}
Java PrimeFinder Class
0
5000000
10000000
15000000
20000000
25000000
30000000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Nanoseconds
Iteration
Java 10, 10k, Primefinder
Primative Class PrimeFinder
C++ PrimeFinder Class
class PrimeFinder
{
private:
int _max_prime;
public:
PrimeFinder(int max_prime);
int find_primes();
};
PrimeFinder::PrimeFinder(int max_prime) {
_max_prime = max_prime;
}
int PrimeFinder::find_primes() {
int num_primes_found = 0;
int n;
for (n = 2; n <= _max_prime; n++) {
int max_divisor = (int) sqrt(n);
bool is_prime = true;
int i;
for (i = 2; i <= max_divisor; i++) {
if (n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
num_primes_found++;
}
}
return num_primes_found;
}
C++ PrimeFinder Class
0
200000
400000
600000
800000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Nanoseconds
Iteration
C++ PrimeFinder
C++ Primative C++ PrimeFinder
Java Prime Finder + SQRT
Java Prime Finder + SQRT
0
5000000
10000000
15000000
20000000
25000000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Nanoseconds
Iteration
Java 10, 10k, Primefinder
Primative Class PrimeFinder SQRT
C++ PrimeFinder + SQRT
0
200000
400000
600000
800000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Nanoseconds
Iteration
C++ PrimeFinder SQRT
C++ PrimeFinder C++ PrimeFinder SQRT
Java IsPrime Method
public class PrimeFinder {
private Boolean is_prime(Integer n) {
Integer max_divisor = (int) sqrt(n);
for (Integer i = 2; i <= max_divisor; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public Integer find_primes() {
Integer num_primes_found = 0;
for (Integer n = 2; n <= max_prime; n++) {
if (is_prime(n)) {
num_primes_found++;
}
}
return num_primes_found;
}
}
Java IsPrime Method
0
5000000
10000000
15000000
20000000
25000000
30000000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Nanoseconds
Iteration
Java 10, 10k, Integer, PrimeFinder, IsPrime
Primative Class PrimeFinder PrimeFinder IsPrime
C++ IsPrime Method
bool PrimeFinder::is_prime(int n) {
int max_divisor = (int) sqrt(n);
for (int i = 2; i <= max_divisor; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int PrimeFinder::find_primes() {
int num_primes_found = 0;
int n;
for (n = 2; n <= _max_prime; n++) {
if (is_prime(n)) {
num_primes_found++;
}
}
return num_primes_found;
}
C++ IsPrime Method
0
200000
400000
600000
800000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Nanoseconds
Iteration
C++ PrimeFinder isPrime
C++ PrimeFinder C++ PrimeFinder isprime
Java 1K tests
0
1000000
2000000
3000000
4000000
5000000
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
Nanoseconds
Iteration
Java 10, 1k, Integer Primefinder
Integer Class PrimeFinder

More Related Content

PDF
Dive Into PyTorch
PPTX
Introduction to PyTorch
PDF
Effective Modern C++ - Item 35 & 36
PDF
Use C++ to Manipulate mozSettings in Gecko
PPT
NS2: Binding C++ and OTcl variables
PPTX
Building High-Performance Language Implementations With Low Effort
PPTX
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
PDF
Bartosz Milewski, “Re-discovering Monads in C++”
Dive Into PyTorch
Introduction to PyTorch
Effective Modern C++ - Item 35 & 36
Use C++ to Manipulate mozSettings in Gecko
NS2: Binding C++ and OTcl variables
Building High-Performance Language Implementations With Low Effort
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Bartosz Milewski, “Re-discovering Monads in C++”

What's hot (20)

PDF
Yampa AFRP Introduction
PPT
20100712-OTcl Command -- Getting Started
PDF
C++ aptitude
PPT
NS2 Classifiers
PDF
JVM Mechanics: Understanding the JIT's Tricks
PPT
Deuce STM - CMP'09
PPTX
PVS-Studio team experience: checking various open source projects, or mistake...
PDF
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
PDF
Lattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWE
PDF
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
PPTX
同態加密
PDF
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
PDF
Lattice Cryptography
KEY
Basic Packet Forwarding in NS2
PPTX
AA-sort with SSE4.1
PDF
Rainer Grimm, “Functional Programming in C++11”
PDF
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PDF
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
PPT
NS2 Object Construction
PPTX
Working with NS2
Yampa AFRP Introduction
20100712-OTcl Command -- Getting Started
C++ aptitude
NS2 Classifiers
JVM Mechanics: Understanding the JIT's Tricks
Deuce STM - CMP'09
PVS-Studio team experience: checking various open source projects, or mistake...
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Lattice-Based Cryptography: CRYPTANALYSIS OF COMPACT-LWE
"PyTorch Deep Learning Framework: Status and Directions," a Presentation from...
同態加密
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Lattice Cryptography
Basic Packet Forwarding in NS2
AA-sort with SSE4.1
Rainer Grimm, “Functional Programming in C++11”
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
NS2 Object Construction
Working with NS2
Ad

Similar to C++ & Java JIT Optimizations: Finding Prime Numbers (20)

PPTX
C vs Java: Finding Prime Numbers
PDF
You will be implementing the following functions. You may modify the.pdf
PPTX
Number_Theory-1 number theory notes for engineering
PPTX
Number theory - Prime Numbers & GCD Algorithms
PPTX
Print prime numbers in a range in different mathotods.pptx
PPTX
10-Lec - Nested IF Statement.pptx
PDF
Buacm 3
PDF
Advances in composite integer factorization
PDF
11 Applied_Mathemathics HANDBOOK.pdf
PDF
accenture Advanced coding questiosn for online assessment preparation
PPTX
Prime and Composite Numbers
PPTX
cryptography Primality Test presentation.pptx
PDF
Python Programming unit4.pdf
PDF
Adopting GraalVM - Scala eXchange London 2018
PPTX
Nbvtalkatbzaonencryptionpuzzles
PPTX
Nbvtalkatbzaonencryptionpuzzles
PDF
Adopting GraalVM - NE Scala 2019
PPT
2010 3-24 cryptography stamatiou
PDF
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
PDF
Please use the code below and make it operate as one program- Notating.pdf
C vs Java: Finding Prime Numbers
You will be implementing the following functions. You may modify the.pdf
Number_Theory-1 number theory notes for engineering
Number theory - Prime Numbers & GCD Algorithms
Print prime numbers in a range in different mathotods.pptx
10-Lec - Nested IF Statement.pptx
Buacm 3
Advances in composite integer factorization
11 Applied_Mathemathics HANDBOOK.pdf
accenture Advanced coding questiosn for online assessment preparation
Prime and Composite Numbers
cryptography Primality Test presentation.pptx
Python Programming unit4.pdf
Adopting GraalVM - Scala eXchange London 2018
Nbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
Adopting GraalVM - NE Scala 2019
2010 3-24 cryptography stamatiou
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
Please use the code below and make it operate as one program- Notating.pdf
Ad

More from Adam Feldscher (7)

PPTX
Java JIT Performance Testing and Results
PPTX
Optimizing Java Notes
PPTX
Java JIT Improvements Research
PPTX
Java JIT Optimization Research
PPTX
Paper summary
PPTX
Optimizing Java
PPTX
Performance Design Patterns 3
Java JIT Performance Testing and Results
Optimizing Java Notes
Java JIT Improvements Research
Java JIT Optimization Research
Paper summary
Optimizing Java
Performance Design Patterns 3

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
PPTX
Construction Project Organization Group 2.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
web development for engineering and engineering
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
UNIT 4 Total Quality Management .pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Mechanical Engineering MATERIALS Selection
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
R24 SURVEYING LAB MANUAL for civil enggi
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Model Code of Practice - Construction Work - 21102022 .pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Well-logging-methods_new................
573137875-Attendance-Management-System-original
Construction Project Organization Group 2.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
additive manufacturing of ss316l using mig welding
web development for engineering and engineering
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks

C++ & Java JIT Optimizations: Finding Prime Numbers

  • 1. Object Comparisons Finding Prime Numbers C++ & Java ADAM FELDSCHER I699 PERFORMANCE DESIGN PATTERNS II 10/16/2019
  • 2. Modifications  All tests are done for primes up to 10k, with 40 iterations  1k had too much noise  Java and C++  Java Primitives vs Wrappers  PrimeFinder Class Invoction  New instance per each iteration  Bring SQRT into for loop comparison  Adding is_prime method
  • 3. Algorithm (Java) for (int iter = 0; iter < max_iter; iter++) { // insert delays long start_time = System.nanoTime(); for (int n = 2; n <= max_prime; n++) {// 2 - 10k int max_divisor = (int) sqrt(n); boolean is_prime = true; for (int i = 2; i <= max_divisor; i++) {// check for divisors if (n % i == 0) { is_prime = false; break; } } if (is_prime) { num_primes_found++; // count primes } } long end_time = System.nanoTime(); // print things num_primes_found = 0; }
  • 4. Java Consistency Check 0 1000000 2000000 3000000 4000000 5000000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 Nanoseconds Iteration Java 10, 10k, normal consistency Run 1 Run 2
  • 6. Java Primitives vs Wrappers (Integer) 0 5000000 10000000 15000000 20000000 25000000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 Nanoseconds Iteration Java 10, 10k, Integer Primative Integer
  • 7. Java PrimeFinder Class public class PrimeFinder { private Integer max_prime; public PrimeFinder(Integer max_prime) { this.max_prime = max_prime; } public Integer find_primes() { Integer num_primes_found = 0; for (Integer n = 2; n <= max_prime; n++) { Integer max_divisor = (int) sqrt(n); Boolean is_prime = true; for (Integer i = 2; i <= max_divisor; i++) { if (n % i == 0) { is_prime = false; break; } } if (is_prime) { num_primes_found++; } } return num_primes_found; } } public static void main(String[] args) { for (Integer iter = 0; iter < max_iter; iter++) { //Start Timer PrimeFinder primeFinder = new PrimeFinder(iter, max_prime); num_primes_found = primeFinder.find_primes(); //End timer & Print }
  • 8. Java PrimeFinder Class 0 5000000 10000000 15000000 20000000 25000000 30000000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 Nanoseconds Iteration Java 10, 10k, Primefinder Primative Class PrimeFinder
  • 9. C++ PrimeFinder Class class PrimeFinder { private: int _max_prime; public: PrimeFinder(int max_prime); int find_primes(); }; PrimeFinder::PrimeFinder(int max_prime) { _max_prime = max_prime; } int PrimeFinder::find_primes() { int num_primes_found = 0; int n; for (n = 2; n <= _max_prime; n++) { int max_divisor = (int) sqrt(n); bool is_prime = true; int i; for (i = 2; i <= max_divisor; i++) { if (n % i == 0) { is_prime = false; break; } } if (is_prime) { num_primes_found++; } } return num_primes_found; }
  • 10. C++ PrimeFinder Class 0 200000 400000 600000 800000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 Nanoseconds Iteration C++ PrimeFinder C++ Primative C++ PrimeFinder
  • 12. Java Prime Finder + SQRT 0 5000000 10000000 15000000 20000000 25000000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 Nanoseconds Iteration Java 10, 10k, Primefinder Primative Class PrimeFinder SQRT
  • 13. C++ PrimeFinder + SQRT 0 200000 400000 600000 800000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 Nanoseconds Iteration C++ PrimeFinder SQRT C++ PrimeFinder C++ PrimeFinder SQRT
  • 14. Java IsPrime Method public class PrimeFinder { private Boolean is_prime(Integer n) { Integer max_divisor = (int) sqrt(n); for (Integer i = 2; i <= max_divisor; i++) { if (n % i == 0) { return false; } } return true; } public Integer find_primes() { Integer num_primes_found = 0; for (Integer n = 2; n <= max_prime; n++) { if (is_prime(n)) { num_primes_found++; } } return num_primes_found; } }
  • 15. Java IsPrime Method 0 5000000 10000000 15000000 20000000 25000000 30000000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 Nanoseconds Iteration Java 10, 10k, Integer, PrimeFinder, IsPrime Primative Class PrimeFinder PrimeFinder IsPrime
  • 16. C++ IsPrime Method bool PrimeFinder::is_prime(int n) { int max_divisor = (int) sqrt(n); for (int i = 2; i <= max_divisor; i++) { if (n % i == 0) { return false; } } return true; } int PrimeFinder::find_primes() { int num_primes_found = 0; int n; for (n = 2; n <= _max_prime; n++) { if (is_prime(n)) { num_primes_found++; } } return num_primes_found; }
  • 17. C++ IsPrime Method 0 200000 400000 600000 800000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 Nanoseconds Iteration C++ PrimeFinder isPrime C++ PrimeFinder C++ PrimeFinder isprime
  • 18. Java 1K tests 0 1000000 2000000 3000000 4000000 5000000 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 Nanoseconds Iteration Java 10, 1k, Integer Primefinder Integer Class PrimeFinder

Editor's Notes

  • #5: Retest in linux….
  • #7: Significant! Scale difference from last
  • #9: Surprising that there’s not more change
  • #11: Gets entirely optimized out
  • #13: Almost no impact
  • #16: Some weirdness, not sure if it jits faster because there are smaller units, or if its just noise. JIT LOG
  • #18: No change
  • #19: 1k has more details but also more noise. Linux