Comparing Python to Java

Gauloran

Kıdemli Moderatör
7 Tem 2013
8,094
585
local
This paper is subjective, and as such, is not meant to be a serious technical discussion of the various merits of these languages. It is my own personal opinion, and we all know what opinions are like.
The purpose of this paper is to provide a perspective for people planning to start their own projects in one or the other of these languages. As such, if you are reading this and have a different paper that you'd like to submit as a counterexample, I will gladly link to it.


Test-By-Test


Python

Kod:
for x in xrange(1000000):     print x
Java

Kod:
public class ConsoleTest {     public static **** main(String[] args) {         for (int i = 0; i < 1000000; i++) {             System.out.println(i);         }     } }
The console test was impressive to me: Java's performance was astonishingly bad. Combined with the long initialisation time, this makes Java a completely unsuitable language for streams-based programming. It might be worth noting here that System.out actually writes to stderr, which is highly confusing (and broken, IMHO) behaviour. Java was definitely not designed with shell scripting or piping in mind.

Python

Kod:
for i in xrange(1000):     x={}     for j in xrange(1000):         x[j]=i         x[j]
Java

Kod:
import java.util.Hashtable;  public class HashTest {     public static **** main(String[] args) {         for (int i = 0; i < 1000; i++) {             Hashtable x = new Hashtable();             for (int j = 0; j < 1000; j++) {                 x.put(new Integer(i), new Integer(j));                 x.get(new Integer(i));             }         }     } }
Here is one of the many places that python benefits from a standard data structure being in C; the "{}" Hashtable is one of the primary reasons I decided to move Twisted Reality to python.

Python

Kod:
f=open('scratch','wb') for i in xrange(1000000):     f.write(str(i)) f.close()
Java

Kod:
import java.io.*;  public class IOTest {     public static **** main(String[] args) {         try {             File f = new File("scratch");             PrintWriter ps = new PrintWriter(new OutputStreamWriter                                              (new FileOutputStream(f)));             for (int i = 0; i < 1000000; i++) {                 ps.print(String.valueOf(i));             }             ps.close();         }         catch(IOException ioe) {             ioe.printStackTrace();         }     } }
Python's I/O is also marginally faster than Java's -- considering that python's interpreter is vastly slower, this is really impressive. To be fair, this is a super-naive implementation of file access in Java. It's stream-based, and not buffered. However, the point of this exercise was mostly to test the most 'natural' way of doing things in each language.

Python

Kod:
for i in xrange(1000):     v=['a','b','c','d','e','f','g']     for j in xrange(1000):         v.append(j)         v[j]
Java

Kod:
import java.util.Vector;  public class ListTest {     public static **** main(String[] args) {         for (int i = 0; i < 1000; i++) {             Vector v = new Vector();             v.addElement("a");             v.addElement("b");             v.addElement("c");             v.addElement("d");             v.addElement("e");             v.addElement("f");             v.addElement("g");             for (int j = 0; j < 1000; j++) {                 v.addElement(new Integer(j));                 v.elementAt(j);             }         }     } }
Java's list syntax is hideous. It turns out (to my surprise) that Vector performs better than the standard [] operator in python... or at least, it doesn't sufficiently outperform it to beat the interpreter speed difference.

Patterns in these Examples

Python is easier to program in than Java. I have had experience with this in the past, but I kept track of these exercises. It took me roughly fifteen minutes to write all of the python examples, and almost an hour to complete the Java equivalents. None of the python programs had any errors in them when first run; I had 3 syntax errors to correct in Java.

Java's ********ation is generally better, and better organised (at least the API doc) but even though I've been using Java for several years, and python for only a month, I found it easy to remember all of the python necessary for this exercise without resorting to the module doc; I had to look up 3 things about IOStreams and URLs in the Java ********ation.

Java programs are longer than their equivalent python programs. In fact, they're approximately 3 times longer, if you're counting bytes in source code (remembering, also, that python programs can run from sources and Java must be compiled first).

Totals for the programs I wrote (source only) were: python, 921 bytes, Java, 2,742 bytes.
Java is faster, and more suitable for "systems-level" programming. I believe that c -> c++ -> Java -> python establishes a nice continuum of systems-oriented, low-level programming to application-oriented, high-level programming.




 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.