Vipin
2009-Jun-23 17:37 UTC
performance improvement in method to strip non-numeral characters from string ??
I need to write a method such that when we pass on a string to it, it returns a substring out of it. returned string is such that it consists of numerals only. All the characters other than numerals are stripped away. e.g. " 2009-05-30 10:25:15 UTC" ==> 20090530102515 I have written the following method for the same. It seems to be working but I am looking for your suggestions / feedback for --> performance improvment --> any bug that i am not able to see def getNumeralString(istr) x = istr.split(//) istr = "" x.each{|a| if a[0] >= ''0''[0] && a[0] <= ''9''[0] istr = istr +a end }; return istr end Thanks for feedback
Frederick Cheung
2009-Jun-23 18:21 UTC
Re: performance improvement in method to strip non-numeral characters from string ??
On Jun 23, 6:37 pm, Vipin <sh.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I need to write a method such that when we pass on a string to it, it > returns a substring out of it. returned string is such that it > consists of numerals only. All the characters other than numerals are > stripped away. > > e.g. > " 2009-05-30 10:25:15 UTC" ==> 20090530102515 >s.gsub(/[^0-9]/,'''') is more concise and probably faster Fred> I have written the following method for the same. > It seems to be working but I am looking for your suggestions / > feedback for > --> performance improvment > --> any bug that i am not able to see > > def getNumeralString(istr) > x = istr.split(//) > istr = "" > x.each{|a| > if a[0] >= ''0''[0] && a[0] <= ''9''[0] > istr = istr +a > end > }; > return istr > end > > Thanks for feedback
Matt Jones
2009-Jun-24 16:18 UTC
Re: performance improvement in method to strip non-numeral characters from string ??
I think Fred''s nailed the solution already, but some more general advice: - I''d *highly* recommend you read Programming Ruby (the Pickaxe book), either online (http://www.rubycentral.com/book/), or purchased from Pragmatic Programmers (http://www.pragprog.com/titles/ruby/programming- ruby). Your original code looks like a classic example of "C programmers can program any language in C", and learning the Ruby idioms will help avoid this sort of thing. - I''d also recommend that you work on relaxing your "speed optimizing" tendencies. Skimming through some of your other posts to various Google Groups shows quite a few discussions most would describe as "premature optimization". While it''s important to be a little concerned about performance (ie, not writing algorithms that are deliberately inefficient), it''s equally destructive to be too concerned about performance. At least at the outset, just write clear code. A surprising amount of the time in Ruby, clear code (Fred''s solution using gsub) is significantly faster than unclear code. --Matt Jones On Jun 23, 1:37 pm, Vipin <sh.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I need to write a method such that when we pass on a string to it, it > returns a substring out of it. returned string is such that it > consists of numerals only. All the characters other than numerals are > stripped away. > > e.g. > " 2009-05-30 10:25:15 UTC" ==> 20090530102515 > > I have written the following method for the same. > It seems to be working but I am looking for your suggestions / > feedback for > --> performance improvment > --> any bug that i am not able to see > > def getNumeralString(istr) > x = istr.split(//) > istr = "" > x.each{|a| > if a[0] >= ''0''[0] && a[0] <= ''9''[0] > istr = istr +a > end > }; > return istr > end > > Thanks for feedback
Vipin
2009-Jun-26 01:17 UTC
Re: performance improvement in method to strip non-numeral characters from string ??
On Jun 24, 9:18 pm, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I think Fred''s nailed the solution already, but some more general > advice: > > - I''d *highly* recommend you read Programming Ruby (the Pickaxe book), > either online (http://www.rubycentral.com/book/), or purchased from > Pragmatic Programmers (http://www.pragprog.com/titles/ruby/programming- > ruby). Your original code looks like a classic example of "C > programmers can program any language in C", and learning the Ruby > idioms will help avoid this sort of thing. > > - I''d also recommend that you work on relaxing your "speed optimizing" > tendencies. Skimming through some of your other posts to various > Google Groups shows quite a few discussions most would describe as > "premature optimization". While it''s important to be a little > concerned about performance (ie, not writing algorithms that are > deliberately inefficient), it''s equally destructive to be too > concerned about performance. At least at the outset, just write clear > code. A surprising amount of the time in Ruby, clear code (Fred''s > solution using gsub) is significantly faster than unclear code. > > --Matt Jones > > On Jun 23, 1:37 pm, Vipin <sh.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I need to write a method such that when we pass on a string to it, it > > returns a substring out of it. returned string is such that it > > consists of numerals only. All the characters other than numerals are > > stripped away. > > > e.g. > > " 2009-05-30 10:25:15 UTC" ==> 20090530102515 > > > I have written the following method for the same. > > It seems to be working but I am looking for your suggestions / > > feedback for > > --> performance improvment > > --> any bug that i am not able to see > > > def getNumeralString(istr) > > x = istr.split(//) > > istr = "" > > x.each{|a| > > if a[0] >= ''0''[0] && a[0] <= ''9''[0] > > istr = istr +a > > end > > }; > > return istr > > end > > > Thanks for feedbackThanks for advice, Matt. I would like to read that book as soon as i get the time from current project. Thanks vipin