From: Robert Klemme Date: 2005-03-19T21:59:51+09:00 Subject: Re: Iterating through a string and removing leading characters "Christian Neukirchen" schrieb im Newsbeitrag news:m2vf7n28y0.fsf@lilith.local... > Florian Gross writes: > >> Randy Kramer wrote: >> >>> * ?? (Is there any method which chops the first character from >>> the string? How efficient is that (especially compared to the >>> method described in the next bullet). >> >> str.slice!(0) >> >> I think this is actually fairly efficient, but I suppose there's a >> memmove involved at the C level. >> >> Perhaps you could set the characters you don't need to '\0' and remove >> all of them after the iteration has ended with a Regexp? > > Better use String#delete! for that, no? For removing \0 chars? Yepp, that seems like an efficient way to do it. For direct removing slice! and String#[]= are most efficient: >> Benchmark.bm do ?> end user system total real => true >> Benchmark.bm do |r| ?> r.report("slice!") do ?> 10000.times { "aaaaaaaaaaaaaa".slice!(0,5) } >> end >> r.report("[]=") do ?> 10000.times { "aaaaaaaaaaaaaa"[0,5]="" } >> end >> end user system total real slice! 0.140000 0.000000 0.140000 ( 0.130000) []= 0.063000 0.000000 0.063000 ( 0.065000) => true >> Kind regards robert