Skip to content

Instantly share code, notes, and snippets.

@afniedermayer
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afniedermayer/f9cea59ceba24858ac0a to your computer and use it in GitHub Desktop.
Save afniedermayer/f9cea59ceba24858ac0a to your computer and use it in GitHub Desktop.
sorting of tuple/pairs
import numpy as np
import copy, time, gc
temp=np.random.rand(500000)
temp2=zip(temp,range(len(temp)))
gc.collect()
t0=time.time()
temp2.sort()
t1=time.time()
print('elapsed time: {} seconds'.format(t1-t0))
# elapsed time: 1.24837398529 seconds
immutable NumericPair{X,Y} <: Number
x::X
y::Y
end
Base.isless(a::NumericPair, b::NumericPair) =
(a.x<b.x) || (a.x==b.x && a.y<b.y)
function sort_numeric_pair()
temp = rand(500_000)
temp2 = [NumericPair(temp[i],i) for i=1:length(temp)]
gc(); @time sort!(temp2)
end
sort_numeric_pair();
sort_numeric_pair(); # elapsed time: 0.091954637 seconds (0 bytes allocated)
function sort_floatint_tuple()
temp = rand(500_000)
temp2 = (Float64,Int64)[(temp[i],i) for i=1:length(temp)]
gc(); @time sort!(temp2)
end
sort_floatint_tuple();
sort_floatint_tuple(); # elapsed time: 6.310294619 seconds (2096124192 bytes allocated, 61.99% gc time)
function sort_anyany_tuple()
temp = rand(500_000)
temp2 = (Any,Any)[(temp[i],i) for i=1:length(temp)]
gc(); @time sort!(temp2)
end
sort_anyany_tuple();
sort_anyany_tuple(); # elapsed time: 2.154703193 seconds (435280224 bytes allocated, 32.04% gc time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment