Python vs. Perl vs. Java vs. C ++ runtimes

xiaoxiao2021-03-06  68

Python vs. Perl vs. Java vs. C runtimes

Source: http://www.flat222.org/mac/bench/

It's dead Jim 09 Sept 2002 OK -! I've had lots of good mail and suggestions for upgrading the benchmarks, or making them more fair (or biased ;-) However, these were only ever meant as a counter example to some other. benchmarks (see the next paragraph), and I see them as so totally flawed that I do not want to spend any more time developing them. Instead I'm going to do some more "real world" style benchmarks. They will appear here At Some Point. this page will not be updated again. Woohoo.

THIS Page IS A Long Overdue Response To The Python / Java Comparison

'A Subjective Analysis of Two High-Level, Object-Oriented Languages' by Glyph Lefkowitz (Now Shortened to [1])

.

The Code Has Been Somewhat Changed, Mainly to Make The Java Code More Natural Code I Was Offen Thinking "That's Not To Do It". I Didn't Feel That for THE Python Code, Which I Suspect Says Good Things About The Simplicity and Clarity Of Python. I've Also Implement The Code In C for Comparison.

It's worth noting that both the original tests, and these ones derived from them are extremely naive;. Most of the tests stand a chance of being no-op'd by a clever compiler (or run time) It would be far more interesting to see them slightly expanded (to things like matrix manipulation, balanced tree generation, etc), and I may tackle that (in my copious free time) in the future Update 2002-05-13:. Came across Doug Bagley's benchmarks here, this is exactly what I was thinking of, so now I dont have to bother doing it.Note that these tests all include the startup time for the JVM. If you are willing to discount startup time and focus on the time spent running the benchmark, you can Deduct About a second from the java scores (and almost Nothing for Pytohn and C ). This is particularly release for the 'NO', 'spesed' and 'native' benchmarks.

All the times are an average of 3 executions, platform information is at the bottom of the page. I increased the number of iterations for some of the benchmarks from [1], for the sole reason to make the graph look better.

.........................

Personal bias disclaimer: I really like Python, Java and C I love the freedom and flexibility of Python (but I hate 'self') I love the raw speed of C , but hate the arcane development process and debugging it I like... Java for The Fact That Alth It's A Bit Slower Than C , It's Much Easier To Work with (OH, AND LARGE Portable Libraries TOO).

Update 10th August 2002, updated the benchmarks on a new machine, updated java versions to 1.4.0_01 as the baseline, and 1.4.1 BETA for something to compare against, got a mail pointing out inconsistant use of range / xrange in the python code which is now fixed Also added perl for fun -. note I'm not a perl programmer, I just hacked these up Corrections are welcome.Update 26th August 2002, Got a mail from Jay Soffian who pointed out Perl has a range operator that. . Updated the results again to see MUCH BETTER Perl Performance - Even Sneaks Past C in The Console Test!

Results Table

pythonperljava1.4.0java1.4.1 RCc console22.933.5633.5837.583.6hash34.8439.126.357.131.23io33.163.913.684.11.04list31.058.532.713.030.19no0.120.050.861.160.04speed31.8114.211.181.630.18native33.973.961.41.670 .09 Total Sum187.88 73.33 49.76 56.3 6.36

Console

The console test is pretty standard, it just dumps data out. The console was redirected to / dev / null to minimise the impact of the efficiency of the terminal. The end result is that everything is well behind C , but Java is especially bad.

PythonPerljavac for x in xrange (1000000):

Print X

For $ i (0 .. 1000000-1) {

Print "$ I / N"

}

Public class test {

Public static void main (String [] args) {

For (int i = 0; i <1000000; i ) {

System.out.println (i);

}

}

}

#include

INT Main (int Argc, char * argv []) {

For (int i = 0; i <1000000; i ) {

Std :: cout << i << std :: end1;

}

}

Hash

The hash test primarily measures the efficiency of the default hash implementation. For Python this is a dictionary, for Perl an associative array, for Java a HashMap, and for C a map. Java suffers an extra penalty here due to the "primitives are not Objects "problem. Thus, Java needs to allocate extra Objectified versions of the integers to install into the map. This" Everything is an Object, except for the fundamental things "dichotomy is my pet hate with Java. This may or may not be relevant . If you commonly want to put primitive types into containers (as I did when doing a lot of Java), this is a fair test showing up a legitimate Java problem. OTOH, if you normally already have real Objects that you want to use, You can v.

PythonPerljavac for i in inreg (6000):

x = {}

For J in Range (1000):

x [j] = i

X [J]

For $ i (0 .. 6000-1) {

% x = ();

For $ j (0 .. 1000-1) {

$ x {$ j} = $ i;

$ x {$ j};

}

}

Import java.util. *;

Public class test {

Public static void main (String [] args) {

For (INT I = 0; i <6000; i ) {

Map x = new hashmap ();

For (int J = 0; J <1000; J ) {

Integer i = new integer (i);

Integer j = new integer (j);

X.PUT (i, j);

x.get (i);

}

}

}

}

#include

#include

INT Main (int Argc, char * argv []) {

For (INT I = 0; i <6000; i ) {

Map x;

For (int J = 0; J <1000; J ) {

x [i] = j;

x [i];

}

}

}

IO

These results were surpising, as I expected all the systems to be much closer. Java's IO system really is as odd as it looks here (although trivial to wrap) .PythonPerlJavaC f = open ( '/ tmp / scratch', 'wb ')

For i in xrange (1000000):

F.Write (STR (i))

f.close ()

Open ($ F, "> / TMP / Scratch);

For $ i (0 .. 1000000-1) {

Print $ f "$ I / n";

}

Close ($ f);

Import java.io. *;

Public Class Test

{

Public static void main (String [] args) {

Try {

FILE F = New File ("/ TMP / Scratch");

PrintWriter PW = New PrintWriter

New bufferedwriter

NEW FileWriter (f)))

For (int i = 0; i <1000000; i ) {

PW.Print (i);

}

PW.Close ();

}

Catch (IOException IoE) {

IoE.PrintStackTrace ();

}

}

}

#include

#include

Using namespace std;

INT Main (int Argc, char * argv []) {

OFSTREAM OUT;

Out.Open ("/ tmp / scratch");

For (int i = 0; i <1000000; i ) {

OUT << i;

}

Out.close ();

}

List

The pattern continues: Python slow, Java not fast, C blazing Note:. I cheated on the C and let it put 'int's in the intial list because I could not be bothered to create a new type Maybe this helps the speed for. C .

PythonPerljavac for i in inning (3000):

v = ['a', 'b', 'c', 'd', 'e', ​​'f', 'g']

For J in Range (1000):

v.append (j)

v [j]

For $ i (0 .. 3000-1) {

@V = ('A', 'B', 'C', 'D', 'E', 'F', 'G');

For $ j (0 .. 1000-1) {

Push (@ v, $ j);

$ V [j];

}

}

Import java.util. *;

Public class test {

Public static void main (String [] args) {

List initial = new arraylist ();

Initial.Add ("a"); Initial.Add ("b");

Initial.Add ("c");

Initial.Add ("D");

Initial.Add ("e");

Initial.Add ("f");

Initial.Add ("g");

For (int i = 0; i <3000; i ) {

List v = new arraylist (initial);

For (int J = 0; J <1000; J ) {

v.add (new integer (j));

v.get (j);

}

}

}

}

#include

#include

INT Main (int Argc, char * argv []) {

Vector initial;

Initial.push_back (1);

Initial.push_back (2);

Initial.push_back (3);

Initial.push_back (4);

Initial.push_back (5);

Initial.push_back (6);

Initial.push_back (7);

For (int i = 0; i <3000; i ) {

Vector v (initial);

For (int J = 0; J <1000; J ) {

v.push_back (j);

v [i];

}

}

}

NO

My favorite of the silly benchmarks. This does nothing, just calls the program. About all it tells you is the overhead for short running tasks (ie 'ls' type utilities). Java has a big hit of about a second, whilst Python and C Are Much Better.

PythonPerljavac public class test {

Public static void main (String [] args) {

}

}

INT Main (int Argc, char * argv []) {

}

Speed

An Empty Loop Iteration. Python and Perl Slow, Everything else Fast.

PythonPerljavac for x in xrange (20000000):

PASS

For $ i (0 .. 20000000-1) {

}

Public class test {

Public static void main (String [] args) {

For (int i = 0; i <20000000; i );

}

}

INT Main (int Argc, char * argv []) {

For (int i = 0; i <20000000; i );

}

Native

This was really shocking as I've been using SWIG for a while in order to control C modules from Python. I rewrote the code from [1] in order to use SWIG as I feel that this is more representative of what people do. As SWIG now supports Java, I used the same interface file for both languages. I expected the Java support, being new, to be inferior to the mature Python support, but the numbers show that JNI is really quite reasonable, at least compared to whatever the equivalent in Python is C of course has a rather large advantage in this test ;-) Update 2002-05-13:.. Got a nice mail from William S Fulton, the Java SWIG maintainer with an explanation of this behaviour He has some benchmarks Of the swig interface speed here

Perl Has Some Really Good Speed ​​Here (Relative to Python At Least)

PythonPerljavac from pymaths import *

Obj = maths ()

For i in xrange (1000000):

Obj.Add (i, 0)

USE Perlmaths;

$ Obj = new perlmaths :: maths ();

For $ i (0 .. 1000000-1) {

$ OBJ-> Add ($ I, 0);

}

Public class test {

STATIC {

System.LoadLibrary ("javamaths");

}

Public static void main (String [] args) {

Maths m = new matHs ();

For (int i = 0; i <1000000; i ) {

M.Add (i, 0);

}

}

}

#include "maths.hpp"

#include

INT Main (int Argc, char * argv []) {

Maths m = maths ();

For (int i = 0; i <1000000; i ) {

M.Add (i, 0);

}

}

% {include "maths.hpp"%}% include "maths.hpp"

ConclusionSstarting from the Shaky Premise That Sestmarks Actually Mean Anything for "Real" Applications, One Conclusion IS That Python Is Really Really Slow (unless ";-)

I found the performance of Python for these benchmarks really surprising as it is at odds to my everyday experience. I've just completed a small demonstration program that does interactive scientific simulations in a VR environment. Whilst both the simulation code and the rendering system are C , the app itself is Python, and used wxPython for the GUI Even the eventloop was inside Python, and the app was running very fast Python is not fast in an absolute sense -.. by absolute I regard C or C as the defacto baseline for comparing execution speed. However, it's fast enough, at least for acting as a glue layer for C objects. Coupled with the freedom and ease of working in Python, it really shines.Java is getting fairly close to C (speedwise). I have not taken the time to compare the speed history of the Java releases, so I dont know if the speed improvments are leveling off or if the remaining gap will be closed. From the results for the 1.4.1 beta, it looks like there Is a good bi T of overhead, Which Should

A final note on [1]. It's very old, and lots of things have changed since then. Even when it was written I would not have accepted most of the Java problems reported in the second half of the page as they did not Match My Experience At All. Today, They Are Even Less Accurate.

For no Other Reason Excet That I Appear To Be On A Soapbox At The Moment, Here Are My Entirely Subjective Thoughts on The Languages ​​- MODULO Perl Which I Dont Know Enough About.

PythonJavaC DevelopmentFast and easy. Very simple to hack things up in. Dynamic typing etc can often result in finding bugs at runtime that would have been found earlier in a statically typed language.Fair. Longwinded syntax makes it slow going, but helps produce solid code. Enforced exception handling, bounds checking and toString help make debugging easy.OK to write in. Templates work well, and the STL is generally OK. Debugging can be a nightmare.Execution speedSlowGood (modulo VM startup). Swing performance is still slugish, but it is also very capable. A little thought (or experience) is important in order to get the best speed.Superb, even when you're not trying.LibrariesGood default libraries. A number of solid free 3rd party non-standard Libraries (IE WXPYTHON). Superb Standard Libraries, Very Broad and Useful.Limited Standard Libraries, But Libraries.Portability Good, Good.very Good.fair, with Experience.Platform

The Software Was Run ON A PC Running Debian Woody. Information and Software Versions Used WERE:

Processor: 0

Vendor_id: GenuineIntel

CPU Family: 6

MODEL: 5

Model Name: Pentium II (Deschutes)

STEpping: 2

CPU MHZ: 349.076

Cache Size: 512 KB

FDIV_BUG: NO

HLT_BUG: NO

SEP_BUG: NO

F00F_Bug: NO

Coma_bug: NO

FPU: YES

FPU_EXCEPTION: YES

CPUID Level: 2

WP: YES

Flags: FPU VME DE PSE TSC MSR PAE MCE CX8 SEP MTR PGE MCA CMOV PAT PSE36 MMX FXSR

BOGOMIPS: 696.32

Perl -V

This is Perl, V5.6.1 Built for i386-linux

Copyright 1987-2001, Larry Wall

Perl May Be copied only Under the Terms of Either the artistic license or the

GNU General Public License, Which May Be found in The Perl 5 Source Kit.

Complete Documentation for Perl, Including Faq Lists, Should Be Found ONTHIS System Using `man perl 'or` perldoc perl'. If you have access to the the

Internet, Point Your Browser At http://www.perl.com/, The Perl Home Page.

Swig -Version

SWIG VERSION 1.3.13U-20020718-1718

Copyright (c) 1995-1998

University of Utah and The Regents of the university of California

CopyRight (C) 1998-2001

University of Chicago

Compiled with cc

Python -V

Python 2.1.3

G --Version

2.95.4

Java -Version

Java Version "1.4.0_01"

Java (TM) 2 Runtime Environment, Standard Edition (Build 1.4.0_01-B03)

Java Hotspot (TM) Client VM (Build 1.4.0_01-B03, MIXED MODE)

Java2 -Version

./makeversions: line 14: /Home/mikec/apps/j2re1.4.1/bin/java: No Such file or directory

Mike Connell

Back (To my main page)

Last Modified: Mon Sep 9 19:01:05 CEST 2002

转载请注明原文地址:https://www.9cbs.com/read-89171.html

New Post(0)