Skip to content

Instantly share code, notes, and snippets.

@bernEsp
Last active September 8, 2017 15:20
Show Gist options
  • Save bernEsp/10bcb8561b5a412047840933c584024b to your computer and use it in GitHub Desktop.
Save bernEsp/10bcb8561b5a412047840933c584024b to your computer and use it in GitHub Desktop.
3 Quick sort versions mystery-> with each and validations, misterio_a -> selects and comparing always original array size and misterio -> initializing variable with selected list of elements
def misterio(array)
value = array.sample
l,r,c = array.select{|e| e < value}, array.select{|e| e > value}, []
l = misterio(l) if l.size > 1
r = misterio(r) if r.size > 1
c << value
(l + c + r).compact
end
def misterio_a(array)
value = array.sample
l,r,c = [], [], []
l = misterio_a(array.select{|e| e < value}) if array.size > 1
r = misterio_a(array.select{|e| e > value}) if array.size > 1
c << value
(l + c + r).compact
end
def mistery(array)
value = array.sample
l,r,c = [], [], []
array.each do |element|
l << element if element < value
r << element if element > value
c << element if element == value
end
l = mistery(l) if l.size > 1
r = mistery(r) if r.size > 1
l + c + r
end
def data
[
(0..1000).to_a.reverse,
(0..1000).to_a.reverse,
(0..1000).to_a.reverse
]
end
def random_data
random_array = (0..3000000).to_a.collect{ Random.new.rand(3000000) }
Array.new(3) { random_array}
end
data= (0..3000000).to_a.collect{ Random.new.rand(3000000) }
def benchmark_two
Benchmark.bmbm do |x|
x.report('misterio: ') { misterio(random_data[0]) }
x.report('misterio_a: ') { misterio_a(random_data[1]) }
x.report('mistery: ') { mistery(random_data[2]) }
end
end
def benchmarkb(data)
Benchmark.bmbm do |x|
x.report('misterio: ') { misterio(data) }
x.report('misterio_a: ') { misterio_a(data) }
x.report('mistery: ') { mistery(data) }
end
end
def benchmark
Benchmark.bm do |x|
x.report('misterio: ') { misterio(data[0]) }
x.report('misterio_a: ') { misterio_a(data[1]) }
x.report('mistery: ') { mistery(data[2]) }
end
end
@bernEsp
Copy link
Author

bernEsp commented Sep 8, 2017

Benchmark with 30 millions of random integers.

irb(main):097:0> benchmark_two

Rehearsal ------------------------------------------------
misterio:     37.560000   0.820000  38.380000 ( 38.476632)
misterio_a:   38.270000   0.900000  39.170000 ( 39.221072)
mistery:      35.680000   0.850000  36.530000 ( 36.585553)
------------------------------------- total: 114.080000sec

                   user     system      total        real
misterio:     37.530000   0.790000  38.320000 ( 38.379192)
misterio_a:   38.050000   0.840000  38.890000 ( 38.951336)
mistery:      35.870000   0.860000  36.730000 ( 36.781475)

=> [#<Benchmark::Tms:0x007fb75102cac8 @label="misterio: ", @real=38.37919199996395, @cstime=0.0, @cutime=0.0, @stime=0.79, @utime=37.53, @total=38.32>, #<Benchmark::Tms:0x007fb7530179c8 @label="misterio_a: ", @real=38.951335999998264, @cstime=0.0, @cutime=0.0, @stime=0.8399999999999999, @utime=38.05000000000001, @total=38.890000000000015>, #<Benchmark::Tms:0x007fb753017ef0 @label="mistery: ", @real=36.78147500002524, @cstime=0.0, @cutime=0.0, @stime=0.8599999999999994, @utime=35.870000000000005, @total=36.730000000000004>]

@bernEsp
Copy link
Author

bernEsp commented Sep 8, 2017

After reviewing the latest benchmark it includes the measure for generating random integers.

@bernEsp
Copy link
Author

bernEsp commented Sep 8, 2017

irb(main):054:0> benchmark_two(data)

Rehearsal ------------------------------------------------
misterio:     11.150000   0.320000  11.470000 ( 11.548518)
misterio_a:   12.100000   0.320000  12.420000 ( 12.473785)
mistery:       9.590000   0.390000   9.980000 ( 10.023963)
-------------------------------------- total: 33.870000sec

                   user     system      total        real
misterio:     10.940000   0.270000  11.210000 ( 11.298831)
misterio_a:   11.770000   0.270000  12.040000 ( 12.125248)
mistery:       9.560000   0.360000   9.920000 (  9.965691)

=> [#<Benchmark::Tms:0x007f955aea3ed0 @label="misterio: ", @real=11.298831000050995, @cstime=0.0, @cutime=0.0, @stime=0.27, @utime=10.940000000000005, @total=11.210000000000004>, #<Benchmark::Tms:0x007f955afa3f38 @label="misterio_a: ", @real=12.12524800002575, @cstime=0.0, @cutime=0.0, @stime=0.27, @utime=11.769999999999996, @total=12.039999999999996>, #<Benchmark::Tms:0x007f955aea3e30 @label="mistery: ", @real=9.96569099999033, @cstime=0.0, @cutime=0.0, @stime=0.3599999999999999, @utime=9.560000000000002, @total=9.920000000000002>]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment