From: Daniel Berger Date: 2006-07-28T01:36:45+09:00 Subject: Re: String#chop slow? REALLY slow? Mat Schaffer wrote: > I just did a quick benchmark to prove something to myself. But I'd like > to get a sanity check from the people on the list. > > Basically I want to drop what will be a trailing "\n" from input. But > it appears that using String#[] and if statements is nearly 200 times > more efficient than chop. Which just seems really weird, so here's the > benchmark. Maybe I'm doing something wrong. > > Does this seem right? Anyone care to comment? As someone else pointed out, you'll probably want to use String#chop! for faster performance, since it uses the current object instead of creating a new one. Also note that str[0..-2] is not quite the same as str.chop when "\r\n" is involved: irb(main):001:0> str = "hello world\r\n" => "hello world\r\n" irb(main):002:0> str[0..-2] => "hello world\r" irb(main):003:0> str.chop => "hello world" I wouldn't think the extra work of checking for "\r\n" would add that much overhead, though. Regards, Dan This communication is the property of Qwest and may contain confidential or privileged information. Unauthorized use of this communication is strictly prohibited and may be unlawful. If you have received this communication in error, please immediately notify the sender by reply e-mail and destroy all copies of the communication and any attachments.