Skip to content

Instantly share code, notes, and snippets.

@rectalogic
Last active August 29, 2015 14:10
Show Gist options
  • Save rectalogic/ed28826ec4ec0798e411 to your computer and use it in GitHub Desktop.
Save rectalogic/ed28826ec4ec0798e411 to your computer and use it in GitHub Desktop.
limit sort
[aw@jello tmp] $ python sort-slice.py
45920256
[aw@jello tmp] $ python sort-heapq.py
5808128
import resource
from random import random
from heapq import nlargest
nlargest(5, (random() for i in xrange(1000000)))
print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
import resource
from random import random
sorted((random() for i in xrange(1000000)), reverse=True)[:5]
print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment