[pypy-dev] Sorting structs

Tuom Larsen tuom.larsen at gmail.com
Mon Sep 26 16:57:49 EDT 2016


Dear list!

I stumbled upon a problem and I was wondering if someone would by so
kind and explain to me what is going on.

The problem is sorting 2 000 000 points by `x` coordinate:

    from random import random
    from time import time

    class point(object):
        def __init__(self, x, y):
            self.x, self.y = x, y

    data = [point(random(), random()) for i in range(2000000)]

    t = time()
    data.sort(key=lambda p:p.x)
    print time() - t

on my machine in runs 8.74s under PyPy 5.4.1 on MacOS. I then try to
sort the points in JavaScript:

    var data = [];
    for (var i=0; i<2000000; i++) {
        data.push({x:Math.random(), y:Math.random()});
    }

    console.time('sorting');
    data.sort(function(a, b) { return a.x - b.x; });
    console.timeEnd('sorting');

and it runs in 3.09s under V8 5.3.

I was just wondering, why is it nearly 3x slower under PyPy than it is
under V8? Is there any way I could make the code run faster?

Thank you in advance!


More information about the pypy-dev mailing list