Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > destructive! operations

Reply
Thread Tools

destructive! operations

 
 
Navindra Umanee
Guest
Posts: n/a
 
      02-20-2005
Hi,

Is it more efficient to use the destructive versions of functions in
Ruby? I know that in Lisp/Scheme destructive counterparts are usually
offered for efficiency reasons.

Can I assume that string.gsub! is preferable to string.gsub when I
know that the side-effect won't be affecting any other code?

I find the Ruby destructive operations sometimes inconvenient to use.
For example strip! might return null where strip would return a
string. However, if it's more efficient, I'd rather use the
destructive version.

Thanks,
Navin.


 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      02-20-2005
Hi

In message "Re: destructive! operations"
on Sun, 20 Feb 2005 17:22:34 +0900, Navindra Umanee <> writes:

|Is it more efficient to use the destructive versions of functions in
|Ruby? I know that in Lisp/Scheme destructive counterparts are usually
|offered for efficiency reasons.
|
|Can I assume that string.gsub! is preferable to string.gsub when I
|know that the side-effect won't be affecting any other code?

Although most (not all) of bang methods are more efficient than their
counterparts, I discourage the use of them unless the performance is
really problem. "Premature optimization is the source of all evil".

matz.


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-20-2005

"Yukihiro Matsumoto" <> schrieb im Newsbeitrag
news: tlab.jp...
> Hi
>
> In message "Re: destructive! operations"
> on Sun, 20 Feb 2005 17:22:34 +0900, Navindra Umanee
> <> writes:
>
> |Is it more efficient to use the destructive versions of functions in
> |Ruby? I know that in Lisp/Scheme destructive counterparts are usually
> |offered for efficiency reasons.
> |
> |Can I assume that string.gsub! is preferable to string.gsub when I
> |know that the side-effect won't be affecting any other code?
>
> Although most (not all) of bang methods are more efficient than their
> counterparts, I discourage the use of them unless the performance is
> really problem. "Premature optimization is the source of all evil".


Although I agree to the latter statement, I beg to differ on the general
remark - at least a bit. I often use this

while ( line = gets )
line.chomp!
# work with line
end

because I know it's going to be potentially invoked often - even in scripts
that are likely to run only on small input files. But for large files where
not every line is processed it easily divides the number of instances
created by a factor of 2.

Kind regards

robert

 
Reply With Quote
 
Caio Tiago Oliveira
Guest
Posts: n/a
 
      02-20-2005
Robert Klemme, 20/2/2005 12:04:
> "Yukihiro Matsumoto" <> schrieb im Newsbeitrag
> news: tlab.jp...
>> In message "Re: destructive! operations"
>> on Sun, 20 Feb 2005 17:22:34 +0900, Navindra Umanee
>> <> writes:
>>
>> |Is it more efficient to use the destructive versions of functions in
>> |Ruby? I know that in Lisp/Scheme destructive counterparts are usually
>> |offered for efficiency reasons.
>> |
>> |Can I assume that string.gsub! is preferable to string.gsub when I
>> |know that the side-effect won't be affecting any other code?
>>
>> Although most (not all) of bang methods are more efficient than their
>> counterparts, I discourage the use of them unless the performance is
>> really problem. "Premature optimization is the source of all evil".

>
>
> Although I agree to the latter statement, I beg to differ on the general
> remark - at least a bit. I often use this
>
> while ( line = gets )
> line.chomp!
> # work with line
> end
>
> because I know it's going to be potentially invoked often - even in
> scripts that are likely to run only on small input files. But for large
> files where not every line is processed it easily divides the number of
> instances created by a factor of 2.


Bad example:

while ( line = gets.chomp )
# work with line
end



 
Reply With Quote
 
Alexander Kellett
Guest
Posts: n/a
 
      02-20-2005
On Feb 20, 2005, at 5:23 PM, Caio Tiago Oliveira wrote:
> Bad example:
>
> while ( line = gets.chomp )
> # work with line
> end


unless ruby cow's the string and
the copy just has a truncating length,
then this requires copying of the line.

Alex



 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      02-20-2005
On Feb 20, 2005, at 10:23 AM, Caio Tiago Oliveira wrote:

> Bad example:
>
> while ( line = gets.chomp )
> # work with line
> end


gets() returns a String or nil. nil does not support chomp(). When
the chomp() is inside the while loop, this isn't an issue.

James Edward Gray II



 
Reply With Quote
 
Navindra Umanee
Guest
Posts: n/a
 
      02-20-2005
James Edward Gray II <> wrote:
> > Bad example:
> >
> > while ( line = gets.chomp )
> > # work with line
> > end

>
> gets() returns a String or nil. nil does not support chomp(). When
> the chomp() is inside the while loop, this isn't an issue.


Often think it would be nice if "" and 0 were treated like nil. Such
functions could then return "". Heck, NilClass.to_s and NilClass.to_i
already return "" and 0 respectively.

Matz talks about premature optimizations. It looks like nil was made
this way for efficiency purposes! Can't say if it was premature
though.

Thanks for the answers!

Cheers,
Navin.



 
Reply With Quote
 
Navindra Umanee
Guest
Posts: n/a
 
      02-20-2005
Navindra Umanee <> wrote:
> Often think it would be nice if "" and 0 were treated like nil. Such
> functions could then return "". Heck, NilClass.to_s and NilClass.to_i
> already return "" and 0 respectively.


"false" too..

Incidentally I tried to make "", 0 and false respond positively to
nil?. While they did, none of the boolean tests looked at nil? to
make their decision. I guess that's the optimized part.

Cheers,
Navin.


 
Reply With Quote
 
mark sparshatt
Guest
Posts: n/a
 
      02-20-2005
Navindra Umanee wrote:
> James Edward Gray II <> wrote:
>
>>>Bad example:
>>>
>>>while ( line = gets.chomp )
>>> # work with line
>>>end

>>
>>gets() returns a String or nil. nil does not support chomp(). When
>>the chomp() is inside the while loop, this isn't an issue.

>
>
> Often think it would be nice if "" and 0 were treated like nil. Such
> functions could then return "".


One problem with this suggestion that comes to mind is, what if there
are any blank lines in the file. then gets.chomp would return "" which
would cause the while loop to exit. Probably not what you want

--
Mark Sparshatt



 
Reply With Quote
 
Navindra Umanee
Guest
Posts: n/a
 
      02-20-2005
mark sparshatt <> wrote:
> One problem with this suggestion that comes to mind is, what if there
> are any blank lines in the file. then gets.chomp would return "" which
> would cause the while loop to exit. Probably not what you want


Point. The chomp would make it not work here. There are many
situations where it could be elegant though.

Cheers,
Navin.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
Pipelining Fixed_pkg operations (VHDL 200x-FT) Divyang M VHDL 3 02-04-2005 03:06 AM
Binary File Operations Jeremy Robbins Perl 0 07-14-2004 09:45 PM
Mathematical Operations in VHDL MtnSurf8 VHDL 1 04-25-2004 06:07 AM
array operations Liang Perl 2 01-05-2004 04:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57