From: kamphausen@... (SKa) Date: 2001-08-24T23:52:26+09:00 Subject: [ruby-talk:20290] Performance of Substrings Dear Ruby Community, I got a problem at hand where I enhanced the Time class to understand an ISO like time notation (w/o the dashes): 19910917 for the 17th of September in 1991. Additionally you might provide a string which also contains the hours, minutes and seconds: 19910917002055 (55 seconds after 2:00 o'clock AM). This is how I implemented it File: isotime.rb ----------------------------------- class Time def Time.iso(str) Time.gm(*iso_unformat(str)) end end def iso_unformat(t) if t.size == 8 [ t[0,4], t[4,2], t[6,2] ] elsif t.size == 14 [ t[0,4], t[4,2], t[6,2], t[8,2], t[10,2], t[12,2] ] end end ----------------------------------- end_of_file: isotime.rb (I hope there are no typos in this: I can't actually paste the code) The problem is that using the profiler (require 'profile') I found that this routine uses a LOT of time. In my case it makes up 30% of the whole run, but of course this depends on what is done. It seems as if the substring operator [] would need a lot of time, and since it is used fairly often here this really shows in overall performance. My question is if there is any reasonably faster way to achieve the same goal. I'd like to use Time objects because I do a lot of comparisons and intervall detections afterwards. Thanks and Regards Stefan