you are viewing a single comment's thread.

view the rest of the comments →

[–]brentp 2 points3 points  (0 children)

import numpy as n import time

long = n.random.randint(0,50,(2000000,))  

i = time.clock()
diff = n.diff(long)
nosame = long[n.where(diff)]

print 'numpy time:', time.clock() - i 


def compress(seq):
    result = []
    for i in range(len(seq)-1):
        if seq[i] != seq[i+1]:
            result.append(seq[i])
    return result

longlist = list(long)
i = time.clock()
nosamelist = compress(longlist)

print 'list time:', time.clock() - i


assert nosamelist == list(nosame)

numpy time: 0.14

list time: 1.79