Java is a beautiful language, and is also supposedly very efficient. Coming from a background of having used Python, I wanted to see the difference between the 2 languages- and from the start I was very impressed by the explicitness and clarity of Java's OOP based syntax. However, I also wanted to test out the performance differences between the languages.
I started off by trying to test the performance difference between the 2 languages by computation speed. For this, I wrote some code in each language- the program attempted to calculate a mathematical problem, and would iterate many times. I won't be adding this code here, but I will say the results- Python was almost 2 times slower (measured by time) than Java. Interesting, but it was expected. After all, the whole reason I wanted to try using Java is due to the amount of people bragging about the computation speed.
Following that, I proceeded to my second test- making HTTP connections to websites, in order to download web pages. For this test, I wrote another test program which would do the same as the last test, except instead of computing a math equation, it would download a web page using an HTTP library.
I ended up writing the following script in Python. It is very straightforward, it iterates 10 times over downloading a web page, then prints the average.
from requests import get
from time import time
# Start the timer
start = time()
# Loop 10 times
for i in range(10):
# Execute GET request
get("https://httpbin.org/get")
# Stop the timer
stop = time()
# Calculate and print average
avg = (stop - start) / 10
print(avg)
# Prints 0.5385, on my system.
For the Java test, I wrote the following piece of code. It is the same test as before, but implemented in Java.
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.util.Objects;
public class Test {
public static String run(String url) throws IOException {
// Code taken from OKHTTP docs
// https://square.github.io/okhttp/
// https://raw.githubusercontent.com/square/okhttp/master/samples/guide/src/main/java/okhttp3/guide/GetExample.java
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return Objects.requireNonNull(response.body()).string();
}
}
public static void main(String[] args) throws IOException {
// Start the timer
long startTime = System.nanoTime();
// Loop 10 times
for (int i = 0; i < 10; i++) {
// Execute GET request
run("https://httpbin.org/get");
}
// Stop the timer
long endTime = System.nanoTime();
// Calculate the average
float average = (((float) (endTime - startTime)) / 1000000000) / 10;
// Print results (1.05035 on my system)
System.out.println(average);
}
}
Huh... that's unexpected. Alas, isn't Java supposed to be faster than Python? I'm shocked to see that Java is almost 2 times slower than Python in this test, but I'm determined to find a conclusion that favors Java. To satisfy this, I decided to re-write the test using the Java default libraries instead of the OkHttp library. I won't be showing the code here since it's pretty long, but I used HttpURLConnection to assist me. My result was still the same, but slightly quicker than with the OkHttp library.
My final test was to do the same as the previous tests, but on http:// websites (in case the slowness occurs due to the SSL connection). My result was still the same- Python was faster by almost 2 times. The only thing I could think of to why this is happening is because the requests Python library would be coded in C, but as you can see from the "Languages" section of their GitHub page, all of the requests library was programmed in pure Python.
I would like to understand why Java is so slow at running HTTP connections, and if I did something wrong with my system setup or Java test code, what should I have written to improve the results? Also, if it's possible, how can a Java HTTP request be sent so that it is faster than its Python requests counterpart?
