RE: code for Computer Language Shootout

Discussion in 'Python' started by Delaney, Timothy C (Timothy), Mar 16, 2005.

  1. Jacob Lee wrote:

    >> # alias methods to avoid repeated lookup
    >> join = ''.join


    I would actually do the alias here sometimes, but give it a
    semantically-useful name ...

    nosep_join = ''.join

    ....

    Tim Delaney
    Delaney, Timothy C (Timothy), Mar 16, 2005
    #1
    1. Advertising

  2. Delaney, Timothy C (Timothy)

    Guest

    Michael Spencer's version is nice, this is a bit shortened version. The
    main() isn't useful for this very short loop, and you can use shorter
    variable names to make lines shorter (this code isn't much readable,
    it's just for the Shootout, "production quality" code has probably to
    be more readable. Code produced by lot of people of a newsgroup isn't
    the normal code usually produced by a single programmer in a limited
    amount of time).
    I've used file(sys.argv[1]) instead of sys.stdin.


    .. import string, itertools, sys
    ..
    .. t = string.maketrans('ACBDGHKMNSRUTWVYacbdghkmnsrutwvy',
    .. 'TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR')
    ..
    .. for h,b in itertools.groupby( file(sys.argv[1]), lambda x: x[0] in
    ">;" ):
    .. if h:
    .. print "".join(b),
    .. else:
    .. b = "".join(b).translate(t, "\n\r")
    .. print "\n".join( b[-i:-i-60:-1] for i in xrange(1, len(b),
    60) )

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

    The Python Mandelbrot program seems to produce a wrong image:

    http://shootout.alioth.debian.org/benchmark.php?test=mandelbrot&lang=python&id=0&sort=fullcpu

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

    This is a shorter and faster version of wordfreq:
    http://shootout.alioth.debian.org/benchmark.php?test=wordfreq&lang=python&id=0&sort=fullcpu

    .. import string, sys
    ..
    .. def main():
    .. f = {}
    .. t = " "*65+ string.ascii_lowercase+ " "*6+
    string.ascii_lowercase+ " "*133
    ..
    .. afilerl = file(sys.argv[1]).readlines
    .. lines = afilerl(4095)
    .. while lines:
    .. for line in lines:
    .. for w in line.translate(t).split():
    .. if w in f: f[w] += 1
    .. else: f[w] = 1
    .. lines = afilerl(4095)
    ..
    .. l = sorted( zip(f.itervalues(), f.iterkeys()), reverse=True)
    .. print "\n".join("%7s %s" % (f,w) for f,w in l)
    ..
    .. main()

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

    This is my shorter and faster version of Harmonic (I hope the use of
    sum instead of the for is okay for the Shootout rules):
    http://shootout.alioth.debian.org/benchmark.php?test=harmonic&lang=python&id=0&sort=fullcpu

    import sys
    print sum( 1.0/i for i in xrange(1, 1+int(sys.argv[1]) ) )

    Bear hugs,
    Bearophile
    , Mar 17, 2005
    #2
    1. Advertising

  3. Delaney, Timothy C (Timothy)

    Jacob Lee Guest

    On Wed, 16 Mar 2005 16:45:53 -0800, bearophileHUGS wrote:

    > Michael Spencer's version is nice, this is a bit shortened version. The
    > main() isn't useful for this very short loop, and you can use shorter
    > variable names to make lines shorter (this code isn't much readable,
    > it's just for the Shootout, "production quality" code has probably to
    > be more readable. Code produced by lot of people of a newsgroup isn't
    > the normal code usually produced by a single programmer in a limited
    > amount of time).
    > I've used file(sys.argv[1]) instead of sys.stdin.
    >


    I don't see what advantage having smaller variable names gives you. IIRC,
    they measure lines of code in logical rather than physical lines.

    >
    > . import string, itertools, sys
    > .
    > . t = string.maketrans('ACBDGHKMNSRUTWVYacbdghkmnsrutwvy',
    > . 'TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR')
    > .
    > . for h,b in itertools.groupby( file(sys.argv[1]), lambda x: x[0] in
    > ">;" ):
    > . if h:
    > . print "".join(b),
    > . else:
    > . b = "".join(b).translate(t, "\n\r")
    > . print "\n".join( b[-i:-i-60:-1] for i in xrange(1, len(b),
    > 60) )
    >


    I benchmarked this, btw - it ran in the same amount of time as the other
    solution. It does have the advantage of being significantly fewer lines of
    code; I suppose that itertools.groupby, while unexpected to someone from a
    language without such niceties in the standard library =), is a better
    solution than duplicating the code (or the function call) to translate,
    reverse, and format the string.

    > ----------------------
    >
    > The Python Mandelbrot program seems to produce a wrong image:
    >
    > http://shootout.alioth.debian.org/benchmark.php?test=mandelbrot&lang=python&id=0&sort=fullcpu
    >


    It's my understanding that they use an automated diff with the outputs. So
    presumably it's generating correct output or it would be listed as
    "Error". I haven't actually checked this, so who knows.

    > ----------------------
    >
    > This is a shorter and faster version of wordfreq:
    > http://shootout.alioth.debian.org/benchmark.php?test=wordfreq&lang=python&id=0&sort=fullcpu
    >
    > . import string, sys
    > .
    > . def main():
    > . f = {}
    > . t = " "*65+ string.ascii_lowercase+ " "*6+
    > string.ascii_lowercase+ " "*133
    > .
    > . afilerl = file(sys.argv[1]).readlines
    > . lines = afilerl(4095)
    > . while lines:
    > . for line in lines:
    > . for w in line.translate(t).split():
    > . if w in f: f[w] += 1
    > . else: f[w] = 1
    > . lines = afilerl(4095)
    > .
    > . l = sorted( zip(f.itervalues(), f.iterkeys()), reverse=True)
    > . print "\n".join("%7s %s" % (f,w) for f,w in l)
    > .
    > . main()
    >


    Cool. I haven't looked at this one, but why don't you test it against
    their sample data, diff it to make sure the output is identical, and send
    it to their mailing list :).

    > ----------------------
    >
    > This is my shorter and faster version of Harmonic (I hope the use of
    > sum instead of the for is okay for the Shootout rules):
    > http://shootout.alioth.debian.org/benchmark.php?test=harmonic&lang=python&id=0&sort=fullcpu
    >
    > import sys
    > print sum( 1.0/i for i in xrange(1, 1+int(sys.argv[1]) ) )
    >


    Yes, the current Python version is an embarrassment. I was already
    planning to send in the one-character patch (s/range/xrange/) when
    submitting the reverse-complement code. This version is probably more
    efficient than an explicit loop, although I doubt the difference is by
    much. I suppose an ounce of profiling is worth a pound of speculation...

    --
    Jacob Lee
    | www.nearestneighbor.net
    Jacob Lee, Mar 17, 2005
    #3
  4. > > . import string, itertools, sys
    > > .
    > > . t = string.maketrans('ACBDGHKMNSRUTWVYacbdghkmnsrutwvy',
    > > . 'TGVHCDMKNSYAAWBRTGVHCDMKNSYAAWBR')
    > > .
    > > . for h,b in itertools.groupby( file(sys.argv[1]), lambda x: x[0] in
    > > ">;" ):
    > > . if h:
    > > . print "".join(b),
    > > . else:
    > > . b = "".join(b).translate(t, "\n\r")
    > > . print "\n".join( b[-i:-i-60:-1] for i in xrange(1, len(b),
    > > 60) )
    > >

    >
    > I benchmarked this, btw - it ran in the same amount of time as the other
    > solution. It does have the advantage of being significantly fewer lines of
    > code; I suppose that itertools.groupby, while unexpected to someone from a
    > language without such niceties in the standard library =), is a better
    > solution than duplicating the code (or the function call) to translate,
    > reverse, and format the string.


    While the use of groupby() is brilliant, I prefer the previous version as a
    demonstration of beautiful, general purpose, plain Python code running at
    full-speed.


    Raymond Hettinger
    Raymond Hettinger, Mar 17, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacob Lee

    code for Computer Language Shootout

    Jacob Lee, Mar 16, 2005, in forum: Python
    Replies:
    14
    Views:
    628
  2. Replies:
    14
    Views:
    504
    Fredrik Lundh
    Nov 30, 2005
  3. Phil Tomson
    Replies:
    25
    Views:
    309
    Gawnsoft
    Jun 20, 2004
  4. Isaac Gouy
    Replies:
    0
    Views:
    112
    Isaac Gouy
    Aug 3, 2004
  5. Replies:
    9
    Views:
    191
    Isaac Gouy
    Mar 29, 2005
Loading...

Share This Page