Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java vs. brainfuck vs. Python.

Name: Anonymous 2007-04-24 18:38 ID:oUkHXcjW

http://en.wikipedia.org/wiki/Brainfuck
http://en.wikipedia.org/wiki/Java
http://en.wikipedia.org/wiki/Python_%28programming_language%29

Now that we're all up to speed, discuss the pros and cons of each language, as well as which you believe to be the superior language.

Name: Anonymous 2007-05-01 10:54 ID:Heaven

>>26,28,31,40
those languages aren't modern.

Name: Anonymous 2007-05-01 11:34 ID:+g9m3NvU

>>36
Let's do something that can't be optimized away:
% seq 1 1000000 | tee sorted | shuf > shuffled
% cat > Sort.hs
module Main where
import Data.List (sort) -- Pure Haskell implementation: http://darcs.haskell.org/ghc-6.6/packages/base/Data/List.hs
main = readFile "shuffled" >>= return . unlines . map show . sort . ((map read) :: [String] -> [Int]) . lines >>= writeFile "unshuffled"
^D
% ghc -O9000 Sort.hs --make -osort
% time sort
./sort  59.82s user 1.70s system 100% cpu 1:01.52 total

I'm too lazy to write something in C, of course.  Oh, and copying the sorted file or generating a list of numbers is cheating.  And this code sucks of course, read is slow as hell.

Name: Anonymous 2007-05-01 21:35 ID:ySgzKHvm

>>27
Try a real application.

However, I was surprised at PHP 5's performance so I've done a more realistic test (n times more realistic than yours, n times less realistic than reality; n is a large number).

It consists of a function f(n) -> list of primes up to n, using a simple algorithm. It involves nested loops, simple Maths and list concatenation. Code available below results. Tested on Win32 for 500000 primes.

PHP 4.3.11: 17.078 s
Perl 5.8.7: 11.453 s
PHP 5.2.2RC2: 7.750 s
Python 2.4.2: 6.437 s
Python 2.4.2 + Psyco: 1.141 s
g++ 3.4.2 MinGW -O0: 0.625 s
g++ 3.4.2 MinGW -O3 -funroll-loops: 0.515 s

Important things to see:
1. PHP 5 has got a massive improvement in performance. Now it outperforms Perl, and it's closer to Python than it's to Perl.
2. Python is still the fastest.
3. Python + Psyco is half as fast as C++ for intensive computation, offering a language that's vastly more powerful and flexible, ridiculously more productive, and easier to learn.
4. CFLAGS JUST KICKED IN, YO!

-----------------------

Sauce:

PHP:

<?php
function Primes($max) {
    for ($i = 1; $i <= $max; ++$i) {
        $prime = true;
        $limit = (int) sqrt($i);
        for ($j = 2; $j <= $limit; ++$j)
            if (!($i % $j)) {
                $prime = false;
                break;
            }
        if ($prime)
            $a[] = $i;
    }
    return $a;
}

Primes(500000);
?>


Perl:

sub Primes {
    @a = ();

    $max = @_[0];
    for ($i = 1; $i <= $max; ++$i) {    
        $prime = 1;
        $limit = int(sqrt($i)) + 1;
        for ($j = 2; $j < $limit; ++$j) {
            if (!($i % $j)) {
                $prime = 0;
                last;
            }
        }
        push @a, $i if $prime;
    }
           
    return @a;
}


Primes(500000);


Python:

from math import sqrt

def Primes(max):
    a = []
    for i in xrange(1, max + 1):
        prime = True
        for j in xrange(2, int(sqrt(i)) + 1):
            if not i % j:
                prime = False
                break
        if prime:
            a += [i]
    return a


Primes(500000)


C++ (by Anonymous):

#include <vector>

std::vector<int> primes(int max) {
  std::vector<int> a;
  int limit(1);
  for (int i(1); i <= max; ++i) {
    bool isPrime(true);
    int newLimit = limit + 1;
    if (newLimit * newLimit <= i) ++limit;
    for (int j(2); j <= limit; ++j) {
      if (!(i % j)) {
        isPrime = false;
        break;
      }
    }
    if (isPrime) a.push_back(i);
  };
  return a;
}

int main() {
  std::vector<int> v = primes(500000);
}

Name: Anonymous 2007-05-01 21:47 ID:eDA9mLes

>>43
The Python version could be optimized a bit with for/else.

def Primes(max):
        a = []
        for i in xrange(1, max + 1):
                for j in xrange(2, int(sqrt(i)) + 1):
                        if not i % j:
                                break
                else:
                        a += [i]
        return a

Name: Anonymous 2007-05-01 21:58 ID:ySgzKHvm

>>44
Thanks, updated:

PHP 4.3.11: 17.078 s
Perl 5.8.7: 11.453 s
PHP 5.2.2RC2: 7.750 s
Python 2.4.2: 6.313 s
Python 2.4.2 + Psyco: 1.140 s
g++ 3.4.2 MinGW -O0: 0.625 s
g++ 3.4.2 MinGW -O3 -funroll-loops: 0.515 s

Psyco didn't benefit from this; the specialized code must be the same.

Name: Anonymous 2007-05-01 22:48 ID:qbOW1DqZ

Haven't you guys ever heard of this? http://shootout.alioth.debian.org/gp4sandbox/

It covers all that, and a whole lot more. I thought everyone and their dog has seen it.

Name: Anonymous 2007-05-02 1:20 ID:jo+cNYf0

>>45
Weird, for me it's the other way around.

Perl 5.8.8: 13.819s
Python 2.4.3, v1: 7.686s
Python 2.4.3, v2: 7.601s
PHP 5.2.1: 6.572s

Probably has something to do with Lin/Win, CFLAGS, and 64bit.

And:
Psyco only runs on Windows and Linux 32-bit Intel-based machines
So that doesn't really matter for me.

Name: Anonymous 2007-05-02 1:26 ID:AmyrZpU7

Here's a slightly rewritten Perl version. Most of the differences are purely aesthetic, but it gets a decent boost simply from converting the inner for loop to foreach (15s to 9s on my machine):

sub Primes {
    $max = shift;
    @a = ();

    RN: foreach $i (1 .. $max) {
        foreach $j (2 .. sqrt $i) {
            next RN unless ($i % $j);
        }

       push @a, $i;
    }

    @a;
}

Primes(500000);

Name: Anonymous 2007-05-02 16:59 ID:1AFBFp9I

>>45
You're using a release candidate of PHP, but not even the lastest stable Python or Perl?

I'd be interested to see the latest stable of each (Python 2.5.1, Perl 5.8.8, and PHP 5.2.1) compared. If you can't resist using unstable, then use the lastest of each, although I think it's a bad idea.

Name: Anonymous 2007-05-02 18:40 ID:uSeM9WGs

>>49
Yeah, sorry, I'm lazy. I used the Python, Perl and PHP I had right now (on my Windows 2000, 2.4.2, 5.8.7 and 4.3.11), but I also downloaded PHP 5.2 because I was curious about its supposed awesome improvement. I know a more serious benchmark should compare the latest of all.

Anyways, I have to admit PHP has got pretty fast. I'd like to see how it fares against Python for large applications, but it's very hard to compare.

Name: Anonymous 2009-01-14 13:38

JAVA fails

Name: Anonymous 2010-11-15 20:51

Name: Anonymous 2010-12-28 4:25

Name: Sgt.Kabu爎肨kiman鬻阃 2012-05-29 0:44

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List