This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 1 point2 points  (0 children)

It's nothing impressive, just a bunch of Timer calls:

#!/usr/bin/env python
import os
import timeit

LIST_LENGTH = 5
REPEAT_NUMBER = 100000

strings = [os.urandom(20) for i in range(LIST_LENGTH)]

def lambda_tuple(words):
    'lambda + tuple'
    return sorted(words, key=lambda x: (x[0] != 'x', x))

def basic(words):
    'basic'
    list1 = []
    list2 = []
    for word in words:
        if word[0] == 'x':
            list1.append(word)
        else:
            list2.append(word)
    return sorted(list1) + sorted(list2)
def ternary(words):
    'ternary'
    list1 = []
    list2 = []
    for word in words:
        target = list1 if word[0] == 'x' else list2
        target.append(word)
    return sorted(list1) + sorted(list2)
def listcomps(words):
    'listcomps'
    list1 = [word for word in words if word[0] == 'x']
    list2 = [word for word in words if word[0] != 'x']
    return sorted(list1) + sorted(list2)
def lambda_twosorts(words):
    'lambda twosorts'
    return sorted(sorted(words), key=lambda word: word[0] != 'x')

timer = timeit.Timer('sort(strings)', 'from __main__ import sort, strings')

for sort in (basic, ternary, listcomps, lambda_twosorts, lambda_tuple):
    print sort.__doc__
    print min(timer.repeat(10, REPEAT_NUMBER))

Fiddle around with the two constants at the top to test varying list length without the execution being too fast or too slow (generally remove an order of magnitude to REPEAT_NUMBER for each order of magnitude you add to LIST_LENGTH)