#!/usr/bin/env ruby # This is one of several little programs to attempt to test the speed of # various means of using an RE to search a string. # This is the default case, which simply checks the string using the RE. # "[" is ASCII 91 s1 = "This is a test." s2 = "Wednesday is another day." s3 = "---*** A Possible Heading" s4 = " * A possible list item" s5 = "A long string with a link hidden within, in other words, [[Web.WikiHome][a link]]." # The following RE is not exactly correct, but I think it will serve for test purposes. # The "-6" below is on the assumption that the RE requires at least 6 characters # to match--I might have that number wrong, but there is a right number. # I'm not at all sure that the code "around" the RE is at all efficient, there # may be much better ways to iterate through the string. # Basically, the idea is, scan through the string looking for a "[", if found, # invoke the RE on the substring starting from the found location to the end. # (Since it already found one "[" the RE only searches for one more at the # beginning of the string. The RE does not attempt to handle the !, which # "escapes" the link. time_start = Time::now # with times 10000.times do (s1.length-6).times do |i| if s1[i] == ?[ s1[i,s1.length] =~ /\A\[(([A-Z]\w*)\.)?(.*)(#([A-Z]\w*))?\](\[(.*)\])?\]/ end end (s2.length-6).times do |i| if s2[i] == ?[ s2[i,s2.length] =~ /\A\[(([A-Z]\w*)\.)?(.*)(#([A-Z]\w*))?\](\[(.*)\])?\]/ end end (s3.length-6).times do |i| if s3[i] == ?[ s3[i,s3.length] =~ /\A\[(([A-Z]\w*)\.)?(.*)(#([A-Z]\w*))?\](\[(.*)\])?\]/ end end (s4.length-6).times do |i| if s4[i] == ?[ s4[i,s4.length] =~ /\A\[(([A-Z]\w*)\.)?(.*)(#([A-Z]\w*))?\](\[(.*)\])?\]/ end end (s5.length-6).times do |i| if s5[i] == ?[ s5[i,s5.length] =~ /\A\[(([A-Z]\w*)\.)?(.*)(#([A-Z]\w*))?\](\[(.*)\])?\]/ end end end puts (Time::now - time_start).to_s puts $&