More embarrassing microbenchmars for D's GC.

bearophile bearophileHUGS at lycos.com
Mon Jun 9 14:03:41 PDT 2008


Leandro Lucarella:
> You can see Python is almost twice faster than D doing allocation (and the
> python example has some intentional overhead to make it as close as D code
> as possible, using more Pythonic code could yield better results).

Note that Psyco can't compile code outside functions (global code is slower even if you don't use Psyco).

Try putting your code into a main() and add a:
from psyco.classes import __metaclass__
At the top.

The code:

import sys
from datetime import datetime

import psyco
psyco.full()
from psyco.classes import __metaclass__

class Node:
        def __init__(self):
                self.data = 0
                self.next = None

def main():
    start = datetime.now()
    list = Node()
    n = list
    for i in xrange(long(sys.argv[1])):
            n.data = i
            n.next = Node()
            n = n.next
    n.next = None
    end = datetime.now()
    delta = datetime.now() - start
    elapsed1 = delta.seconds * 1000000 + delta.microseconds

    start = datetime.now()
    p = list
    while p:
            p.data += 1
            p = p.next
    delta = datetime.now() - start
    elapsed2 = delta.seconds * 1000000 + delta.microseconds

    print "Fill:  %lu usec" % elapsed1
    print "Inc:   %lu usec" % elapsed2
    print "Total: %lu usec" % (elapsed1 + elapsed2)

main()

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

Original: 9.32 s
Just moving code into a function: 8.39 s
the same, using Psyco: 1.94 s

Bye,
bearophile



More information about the Digitalmars-d mailing list