Dashboard > People > Terence Parr > 2007 > September > 21 > Mantra coming along (speed test vs Python)
  Terence Parr Log In | Sign Up   View a printable version of the current page.  
  Mantra coming along (speed test vs Python)
Added by Terence Parr, last edited by Terence Parr on Sep 21, 2007  (view change)
Labels: 
(None)

Mantra is coming along nicely. Added type annotations, but am not doing anything with them yet.  Without much optimization (and huge amounts of memory allocation), Mantra loop and list append are looking good:

a = [];
1..5000000:{int i | a += i;};

The equivalent python:

a = [];
for i in range(5000000):
        a.append(i);

My unscientific wallclock measurements on my dual cpu mac (powerpc) with Java 1.5 shows Java doing about 3.0s vs 3.9s in python.  I am using way more memory though I think.

When I make use of type info manually converting the += operator to a direct list.add operation rather than general message send, it goes much faster: 2.6s.  I simply converted:

main_locals.a.invoke("+=",new mobject[]{closure1_args.i});

to

((mlist)main_locals.a).add(closure1_args.i);

On the other hand, python's highly optimized file I/O kicks Mantra's butt right now.  Here is my word frequency code:

// for each filename, generate a histogram
wfreq = map();

args:{ string filename |
    f = File(filename);
    f => words() => { string w |
        c=wfreq[w];
        if ( c is null ) wfreq[w]=mutint(1);
        else c++;
    };
};

pairs = wfreq.items(); // return list of 2-tuples (key/value pairs)
pairs.sort({int x, int y | return x[1].compareTo(y[1]);});
pairs.backwards():{list p | print(p[1]); print(" "); println(p[0]);};

Processing a 5M file of text, takes about 3.6s in Mantra, but only 1.1s in Python. Here's the python code:

import sys
import string

freqtable = {}

def processfile(file, ftable):

    infile = open(file, 'r')

    for line in infile:
        words = line.split()
        for w in words:
            try:
                ftable[w] = ftable[w] + 1
            except KeyError:
                ftable[w] = 1

    infile.close()

for f in sys.argv[1:]:
    processfile(f, freqtable)

wordlist = freqtable.items()
wordlist.sort((lambda x, y : cmp(x[1], y[1])))
wordlist.reverse()

for p in wordlist:
    print p[0], p[1]

AFAIK IO in Python is not particularily optimized - it's a rather thin wrapper around libc. I think perl does nasty hacks and can be even faster than C for IO intensive tasks.

Regarding the first example: does 1..5000000 create an actual list of 5m elements (like range(5000000) in Python) or is this a cheap interval object? You may try to use xrange().

interesting info. thanks. The a..b range is just a range object. On left of the => pipe operator, it gets converted to a stream of ints. No list until I do the +=.

You'll probably want to try equivalent code as well:

[i for i in range(50000)]

try xrange too (range is set to behave like xrange, and the xrange name is set to disappear in future releases)

cheers

September 2007
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            

Mantra has type annotations and delegates now!
Heterogeneous AST node types

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators