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 7, 2017

       user     system      total        real
misterio:   0.000000   0.000000   0.000000 (  0.004538)
misterio_a:   0.000000   0.000000   0.000000 (  0.002267)
mistery:   0.010000   0.000000   0.010000 (  0.006095)
=> [#<Benchmark::Tms:0x007fd9bdd69f68 @label="misterio: ", @real=0.004537999979220331, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9be37cd38 @label="misterio_a: ", @real=0.002267000003485009, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9be337620 @label="mistery: ", @real=0.0060950000188313425, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000231, @total=0.010000000000000231>]
irb(main):942:0> benchmark
       user     system      total        real
misterio:   0.000000   0.000000   0.000000 (  0.004100)
misterio_a:   0.000000   0.000000   0.000000 (  0.003867)
mistery:   0.000000   0.010000   0.010000 (  0.012541)
=> [#<Benchmark::Tms:0x007fd9ba9d2ad8 @label="misterio: ", @real=0.004099999991012737, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9be2fd650 @label="misterio_a: ", @real=0.00386699999216944, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9be2a7c50 @label="mistery: ", @real=0.012541000003693625, @cstime=0.0, @cutime=0.0, @stime=0.010000000000000009, @utime=0.0, @total=0.010000000000000009>]
irb(main):943:0> benchmark
       user     system      total        real
misterio:   0.010000   0.000000   0.010000 (  0.014362)
misterio_a:   0.000000   0.000000   0.000000 (  0.004306)
mistery:   0.000000   0.000000   0.000000 (  0.002405)
=> [#<Benchmark::Tms:0x007fd9ba983820 @label="misterio: ", @real=0.014361999987158924, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.009999999999999787, @total=0.009999999999999787>, #<Benchmark::Tms:0x007fd9ba1d1d20 @label="misterio_a: ", @real=0.004306000017095357, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9bdd0c520 @label="mistery: ", @real=0.0024049999774433672, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
irb(main):944:0> benchmark
       user     system      total        real
misterio:   0.000000   0.000000   0.000000 (  0.004393)
misterio_a:   0.010000   0.000000   0.010000 (  0.002032)
mistery:   0.000000   0.000000   0.000000 (  0.003942)
=> [#<Benchmark::Tms:0x007fd9bdcc4c48 @label="misterio: ", @real=0.00439300001016818, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9bdc7f0a8 @label="misterio_a: ", @real=0.0020320000185165554, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000231, @total=0.010000000000000231>, #<Benchmark::Tms:0x007fd9ba9697e0 @label="mistery: ", @real=0.003942000010283664, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
irb(main):945:0> benchmark
       user     system      total        real
misterio:   0.000000   0.000000   0.000000 (  0.004238)
misterio_a:   0.000000   0.000000   0.000000 (  0.002142)
mistery:   0.010000   0.000000   0.010000 (  0.002216)
=> [#<Benchmark::Tms:0x007fd9bdc31588 @label="misterio: ", @real=0.004237999994074926, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9bdc10338 @label="misterio_a: ", @real=0.002142000012099743, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9bdbb2b98 @label="mistery: ", @real=0.0022160000225994736, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.009999999999999787, @total=0.009999999999999787>]
irb(main):946:0> benchmark
       user     system      total        real
misterio:   0.000000   0.010000   0.010000 (  0.003958)
misterio_a:   0.000000   0.000000   0.000000 (  0.002396)
mistery:   0.000000   0.000000   0.000000 (  0.003961)
=> [#<Benchmark::Tms:0x007fd9be2569e0 @label="misterio: ", @real=0.0039579999865964055, @cstime=0.0, @cutime=0.0, @stime=0.010000000000000009, @utime=0.0, @total=0.010000000000000009>, #<Benchmark::Tms:0x007fd9be214ea0 @label="misterio_a: ", @real=0.0023959999962244183, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9be1c34b0 @label="mistery: ", @real=0.003960999980336055, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
irb(main):947:0> benchmark
       user     system      total        real
misterio:   0.010000   0.000000   0.010000 (  0.002577)
misterio_a:   0.000000   0.000000   0.000000 (  0.004312)
mistery:   0.000000   0.000000   0.000000 (  0.004161)
=> [#<Benchmark::Tms:0x007fd9bdb6b6a8 @label="misterio: ", @real=0.0025769999774638563, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000231, @total=0.010000000000000231>, #<Benchmark::Tms:0x007fd9ba179f80 @label="misterio_a: ", @real=0.0043120000045746565, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9ba1383c8 @label="mistery: ", @real=0.004161000018939376, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
irb(main):948:0> benchmark
       user     system      total        real
misterio:   0.010000   0.000000   0.010000 (  0.005976)
misterio_a:   0.000000   0.000000   0.000000 (  0.002250)
mistery:   0.000000   0.000000   0.000000 (  0.003188)
=> [#<Benchmark::Tms:0x007fd9ba0f02d0 @label="misterio: ", @real=0.005976000014925376, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.009999999999999787, @total=0.009999999999999787>, #<Benchmark::Tms:0x007fd9ba0ea790 @label="misterio_a: ", @real=0.0022499999904539436, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9ba0a8520 @label="mistery: ", @real=0.003188000002410263, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
irb(main):949:0> benchmark
       user     system      total        real
misterio:   0.000000   0.000000   0.000000 (  0.002363)
misterio_a:   0.010000   0.000000   0.010000 (  0.004333)
mistery:   0.000000   0.000000   0.000000 (  0.005528)
=> [#<Benchmark::Tms:0x007fd9ba064d48 @label="misterio: ", @real=0.002363000006880611, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9be13c488 @label="misterio_a: ", @real=0.004333000018959865, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000231, @total=0.010000000000000231>, #<Benchmark::Tms:0x007fd9be0ea4d0 @label="mistery: ", @real=0.005528000008780509, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
irb(main):950:0> benchmark
       user     system      total        real
misterio:   0.000000   0.000000   0.000000 (  0.002326)
misterio_a:   0.000000   0.000000   0.000000 (  0.001902)
mistery:   0.010000   0.000000   0.010000 (  0.003791)
=> [#<Benchmark::Tms:0x007fd9bda5c2d0 @label="misterio: ", @real=0.0023259999870788306, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9bda16898 @label="misterio_a: ", @real=0.0019019999890588224, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fd9be04e260 @label="mistery: ", @real=0.003790999995544553, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.009999999999999787, @total=0.009999999999999787>]

@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