Home arrow Tricks, Tips, and Help arrow Advanced Tips arrow Ruby performance tests 1.8 1.9 insertion sort
Ruby performance tests 1.8 1.9 insertion sort Print E-mail
I am brushing up on some of my old algorithm books and have been benchmarking them in ruby 1.8.6 and 1.9.1

Basically I have both installed and ruby19 is the alias for ruby 1.9.1. The server I am running is currently dedicated and I put the cat /proc/cpu in case anyone is interested below as well.

In my sort algorithms I am placing an array of random integers length n (passed as an argument) randomized from 0 to n and using insertion sort to sort. I run it m times, so i run

ruby insort.rb n m
This mean a sorting of n elements from 0-n and the test is run m times

Each run I generate a new array so the data sets are different and the sort difficulty is random

Here are some runs I did

[jared@143086-web1 testc]$ ruby insort.rb 100 100
insertion_sort: 0.00369423ms average
[jared@143086-web1 testc]$ ruby19 insort.rb 100 100
insertion_sort: 0.00095015904ms average

[jared@143086-web1 testc]$ ruby insort.rb 500 100
insertion_sort: 0.05235899ms average
[jared@143086-web1 testc]$ ruby19 insort.rb 500 100
insertion_sort: 0.01686314909ms average

[jared@143086-web1 testc]$ ruby insort.rb 1000 20
insertion_sort: 0.20634605ms average
[jared@143086-web1 testc]$ ruby19 insort.rb 1000 20
insertion_sort: 0.08078076745ms average

Here is a run with a pretty big data set for insertion sort.

[jared@143086-web1 testc]$ ruby insort.rb 5000 5
insertion_sort: 4.2183736ms average
[jared@143086-web1 testc]$ ruby19 insort.rb 5000 5
insertion_sort: 1.2406564422ms average

[jared@143086-web1 testc]$ ruby insort.rb 10000 1
insertion_sort: 17.899982ms average
[jared@143086-web1 testc]$ ruby19 insort.rb 10000 1
insertion_sort: 4.949758742ms average



I don't have the time to do a true analysis, but as far as I can tell with this algorithm the ruby 1.9 compiler seems to be 10 to 100 times fater with smaller data sets, and when the data set gets larger it seems to converge on about 2 to 4 times faster.

I pasted my code below, I'm not optomizing it, just writing each algorithm as quick as I can!


repeats = ARGV[1].to_i
functions = [:insertion_sort]
a = []

def insert a, pos, value
        i = pos - 1
        while i >= 0 and a[i] > value do
                a[i+1] = a[i]
                i-=1
        end
        a[i+1] = value
end
def insertion_sort a
        (1..(a.length-1)).each do |i|
                        insert(a, i, a[i])
        end
        a
end
functions.each do |f|
        total_time = 0
        repeats.times do
                a = []
                (1..top).each do |e|
                        a << rand(e)
                end
                time1 = Time.now
                send f, a
                time2 = Time.now
                total_time += (time2-time1)
        end
        puts "#{f}: #{total_time/repeats.to_f}ms average"
end


processor       : 1
vendor_id       : AuthenticAMD
cpu family      : 15
model           : 67
model name      : Dual-Core AMD Opteron(tm) Processor 1212
stepping        : 3
cpu MHz         : 1000.000
cache size      : 1024 KB
physical id     : 0
siblings        : 2
core id         : 1
cpu cores       : 2
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 1
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow pni cx16 lahf_lm cmp_legacy svm extapic cr8legacy ts fid vid ttp tm stc
bogomips        : 2011.53


 
Next >

Jibwa Work Samples

Contact Jibwa LLC

Under Construction

Jibwa.com is under construction. Watch out for broken links, missing pages, potholes and bulldozers. We apologize for the temporary inconvenience - Jibwa.com Staff

News and Updates

Flex 4 Pediatrics One

Recently Jibwa LCC published demonstration videos and a new website design for Pediatrics One Clinical Management software built on Flex Flash Builder...
Read More ...

Eclectic Flea Simple Business Site

Using hand written materials and some photos we managed to create a simple site for Tucson's artsy thrift store. The Eclectic Flea ...
Read More ...

Flash Builder 2 Release changes from beta

I am moving from (flex) Flash Builder Beta 2 to Flash Builder Release Stable and keeping notes on changes I've had to make to my code. 1.) mx names...
Read More ...

Radiology Gallery

Jibwa and Tripwirearts have built and  launched a new website with Dr Benjamin Strong. radiologygallery.com for radiology continuing medical educ...
Read More ...