Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences in performance between Java and Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what are the differences in performance between Java and Python?". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the differences between the performance of Clipper Java and Python?"

This experiment

I decided to do matrix multiplication in all three languages. The size of the matrix is 0 x 2048 (that is, the multiplication and addition of each matrix is 0. 0), and I filled them with random values between 2048 and 2048 (the effect of using random values instead of using identical matrices in all three languages is negligible). I ran each experiment five times and calculated the average running time.

C code

# include # define n 2048 double A [n] [n]; double B [n] [n]; double C [n] [n]; int main () {/ / populate the matrices with random values between 0.0 and 1.0 for (int I = 0; I < n; iTunes +) {for (int j = 0; j < n) ) {A [I] [j] = (double) rand () / (double) RAND_MAX; B [I] [j] = (double) rand () / (double) RAND_MAX; C [I] [j] = 0;} struct timespec start, end; double time_spent; / / matrix multiplication clock_gettime (CLOCK_REALTIME, & start) For (int I = 0; I < n; iTunes +) {for (int j = 0; j < n; jacks +) {for (int k = 0; k < n; knot +) {C [I] [j] + = A [I] [k] * B [k] [j];} clock_gettime (CLOCK_REALTIME, & end) Time_spent = (end.tv_sec-start.tv_sec) + (end.tv_nsec-start.tv_nsec) / 1000000000.0; printf ("Elapsed time in seconds:% f\ n", time_spent); return 0;}

Java code

Import java.util.Random; public class MatrixMultiplication {static int n = 2048; static double [] [] A = new double [n] [n]; static double [] [] B = new double [n] [n]; static double [] [] C = new double [n] [n]; public static void main (String [] args) {/ / populate the matrices with random values between 0.0 and 1.0 Random r = new Random () For (int I = 0; I < n; for +) {A [I] [j] = r.nextDouble (); B [I] [j] = r.nextDouble (); C [I] [j] = 0;}} long start = System.nanoTime () / / matrix multiplication for (int I = 0; I < n; iTunes +) {for (int j = 0; j < n; jacks +) {for (int k = 0; k < n; knot +) {C [I] [j] + = A [I] [k] * B [k] [j] } long stop = System.nanoTime (); double timeDiff = (stop-start) * 1eMur9; System.out.println ("Elapsed time in seconds:" + timeDiff);}}

Python code

Import random import time n = 2048 # populate the matrices with random values between 0.0 and 1.0A = [[random.random () for row in range (n)] for col in range (n)] B = [[random.random () for row in range (n)] for col in range (n)] C = [[0 for row in range (n)] for col in range (n)] start = time.time () # matrix multiplication for i in range (n): for j in range (n): for k in range (n): C [I] [j] + = A [I] [k] * B [k] [j] end = time.time () print ("Elapsed time in seconds% 0.6f"% (end-start)) how to compile and run # C gcc MatrixMultiplication.c-o matrix. / matrix # Java javac MatrixMultiplication.java java MatrixMultiplication # Python python MatrixMultiplication.py

How to compile and run

# C gcc MatrixMultiplication.c-o matrix. / matrix # Java javac MatrixMultiplication.java java MatrixMultiplication # Python python MatrixMultiplication.py

Running time

According to these results, C is 2.34times slower than Java, and Python is 33.34times slower than Java.

Wait! Shouldn't C be the fastest?

In fact, this is an unfair comparison. When we compile a Java program, the Java JIT (just-in-time) compiler automatically performs optimizations even without any optimization flags. However, for GCC (compiling C programs), this is not the case, and we must explicitly set the optimization flag.

Therefore, I used the-O2 and-O3 optimization flags when compiling the C program, and experimented again.

Gcc-O2 MatrixMultiplication.c-o matrix./matrixgcc-O3 MatrixMultiplication.c-o matrix./matrix

New elapsed time

Today, Java code is 1.69 times slower than C [- O3], while Python code is 56 times slower. I made the right decision (or luckily: -) and chose C over other programming languages.

Summarize the results

Discussion result

Python is relatively slow because C is compiled and Python is interpreted. The compiler converts C code to machine code at once. On the other hand, the interpreter must read, interpret, and execute each line of code and update the machine state (which adds a lot of overhead). When a program is compiled into machine code, CPU can execute it directly. However, when it comes to the interpreter, CPU will run the interpreter, and the interpreter itself will execute the program. (if you are interested in compilers and interpreters, please read the wonderful article by Vaidehi Joshi.)

This is what makes Python very flexible. Python sacrifices a little bit of performance to provide more flexibility / advanced programming capabilities (variables cannot be initialized to n = 100 without specifying data types in C, but can be initialized in Python).

JIT (Java compiler) lies between C and Python. The first time the code is executed, it will be interpreted. However, when a piece of code executes frequently, it is compiled into machine code in real time, and the compiled version is used for further execution.

At this point, I believe you have a deeper understanding of "what are the differences in performance between Java and Python?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report