#!/usr/local/bin/ruby # all_test.rb # # A Ruby implementation of the ScriptPerf embedded scripting language # tests, written by David Jeske. # # Ruby Homepage: http://www.ruby-lang.org/en/ # # July 18, 2000 # # ----------------------------------------------- def loop_test(count) count.times do 10.times do # do nothing end end end # the "for" iterator has slightly slower performance: def loop_test_iter(count) for i in 0..count for j in 0..10 end end end # ----------------------------------------------- def index_test(count) table = {} for i in 0..count table[i] = i end for i in 0..count b = table[i] end end # ----------------------------------------------- def index_string_test(count) table = {} for i in 0..count table["some_string"] = i end for i in 0..count b = table["some_string"] end end # ----------------------------------------------- def nested_int_test(count) n = 0 for a in 0..count n = n + 34 for b in 0..count n = n + 9 for c in 0..count n = n + 5 for d in 0..count n = n + 3 for e in 0..count n = n - 100 end end end end end end # ----------------------------------------------- def nested_int_test_blocks(count) n = 0 count.times { |a| n = n + 34 count.times { |b| n = n + 9 count.times { |c| n = n + 5 count.times { |d| n = n + 3 count.times { |e| n = n - 100 } } } } } end # ----------------------------------------------- def list_append_test(count) a = [] for i in 0..count a.push(5) end return a end $a_list = list_append_test(500000) def list_traverse_test(count) for i in $a_list a = 1 end end # ----------------------------------------------- def file_line_test(count) fp = open("testfile.dat") fp.each_line { } end def file_line_split_test(count) fp = open("testfile.dat") fp.each_line { |line| a,b = line.split(' ') } end def file_line_re_test(count) fp = open("testfile.dat") fp.each_line { |line| line =~ /([0-9]+) ([a-z]*)/ a = $1 b = $2 } end # ----------------------------------------------- def system_test(count) i = 0 while i < count do system("echo test > /dev/null") i+=1 end end # ----------------------------------------------- def basic_fp_test(basearith) i = 0 j = 3.14159265359 k = 3.14159265359 while i < basearith do j = (j * (1/2.71828182846)) + 2.71828182846 k = (k / 2.71828182846) - 2.71828182846 i += 1 end end # ----------------------------------------------- def complex_fp_test(count) i = 0 j = 0 while i < count do j += Math.sin( 3.14159265359 ) j += Math.sqrt ( 69.283848 ) i += 1 end end # ----------------------------------------------- def dothisB(a,b) a end def returntest(count) for i in 0..count a = dothisB(1,2) end end # ----------------------------------------------- def dothisB2(a,b) return a,b end def return2test(count) for i in 0..count a,b = dothisB2(1,2) end end # ----------------------------------------------- def dothis(a) a end def functest(count) for i in 0..count x = dothis(10) end end # ----------------------------------------------- class Foo def dothis(a) a end end def objecttest(count) obj = Foo.new for i in 0..count obj.dothis(10) end end ##################################################################3 # a timing function def timefunk(label, f, count) s = Time.now f.call(count) e = Time.now print(label, " ", count, " elapsed: ", e-s, "\n") end #timefunk("loop_test", method(:loop_test), 500000) #timefunk("index_test", method(:index_test), 1000000) #timefunk("index_string_test", method(:index_string_test), 1000000) #timefunk("nested_int_test", method(:nested_int_test), 23) #timefunk("system_test", method(:system_test), 5000000) #timefunk("basic_fp_test", method(:basic_fp_test), 3000000) #timefunk("complex_fp_test", method(:complex_fp_test), 2000000) #timefunk("returntest", method(:returntest), 5000000) #timefunk("functest", method(:functest), 5000000) #timefunk("objecttest", method(:objecttest), 5000000) #timefunk("list_append_test", method(:list_append_test), 50000) #timefunk("list_traverse_test", method(:list_traverse_test), 500000) timefunk("file_line_test", method(:file_line_test), 1) timefunk("file_line_split_test", method(:file_line_split_test), 1) timefunk("file_line_re_test", method(:file_line_re_test), 1)