Sitemap
Subham Sarangi

Subham Sarangi

Nihilist, Occasional reader, Realist, Dabbler in Linguistics, Skeptic, Computer Engineer; Atheist.

Jython: a superhero team-up. Python and Java.

3 min readJan 31, 2024

Python is popular but sometimes, it can be 100 times slower than some lower-level languages. I wanted to find out if there was a way to keep Python’s cool features and improve its speed.

Various implementations of Python exist. Among them, CPython (written in C) is the original and the most widely used. When you type python at the console you are most probably running CPython. Some other runtimes you may have heard are Jython (written in Java), IronPython (written for .NET), and PyPy (written in Python).

Intrigued by the possibility of leveraging Java’s speed within a Python context, I dipped my toes into Jython. My aim was to create a Java method and call it within Python code, hoping to observe performance enhancements using this dynamic duo.

Press enter or click to view image in full size
generated using leonardo.ai

To gauge the impact, I decided to compare the execution time of checking primality for a large number using Jython against traditional Python and Java implementations.

I began by writing a simple primality checking method in Java:

// JavaPrimalityChecker.java
public class JavaPrimalityChecker {
public static boolean isPrime(long num) {
if (num < 2) {
return false;
}
for (long i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

After compiling the Java code, I can move on to creating a Python script that would call this Java method. Since we are using jython, we can just normally import this class in python and call the method isPrime on our ridiculously huge prime number.

# PrimalityChecker.py
import JavaPrimalityChecker
from time import clock

number = 9223372036854775783
start_time = clock()
print(str(number) + " is prime: " + str(JavaPrimalityChecker.isPrime(number)))
end_time = clock()
print("Execution time: " + str(end_time - start_time) + " seconds")

I am capturing the running time using the clock method of time library.

Get Subham Sarangi’s stories in your inbox

Join Medium for free to get updates from this writer.

Running the Jython script yielded a result, indicating that the Java method was successfully invoked from Python:

This took just above 29 seconds in my desktop.

Next, I wrote a program to check the number directly in Java:

// JavaMainPrime.java
public class JavaMainPrime {
public static boolean isPrime(long num) {
if (num < 2) {
return false;
}
for (long i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

public static void main(String[] args) {
long startTime = System.currentTimeMillis();
boolean result = isPrime(9223372036854775783L);
long endTime = System.currentTimeMillis();
System.out.println(9223372036854775783L + " is prime: " + result);
System.out.println("Execution time: " + (endTime - startTime) + " milliseconds");
}
}

After compiling and executing the Java code, the results were as follows:

more than 31 seconds

It seems that Java is slower than jython. What is happening here?

Lets move on to writing a pure Python program for finding out if a number is prime:

# PythonPrimalityChecker.py
from time import perf_counter

n = 9223372036854775783
is_prime = True
start_time = perf_counter()

for i in range(2, int(n**0.5) + 1):
if (n % i) == 0:
is_prime = False
break

end_time = perf_counter()
print(f"{n} is prime: {is_prime}")
print(f"Execution time: {end_time - start_time} seconds")

Running this Python script, however, proved to be inefficient and time-consuming:

As it was still running after 10 minutes, I lost my patience and decided to call it a day.

This little experiment showcased the potential benefits of using Jython to combine Java’s performance with Python’s ease of use.

--

--

Subham Sarangi
Subham Sarangi

Written by Subham Sarangi

Nihilist, Occasional reader, Realist, Dabbler in Linguistics, Skeptic, Computer Engineer; Atheist.