[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Caching eval() for reuse to gain performance ?

Neville Burnell

6/27/2005 7:17:00 AM

>> p = eval "lambda{#{c}}"

Thanks Kent

-----Original Message-----
From: Kent Sibilev [mailto:ksruby@gmail.com]
Sent: Monday, 27 June 2005 4:23 PM
To: ruby-talk ML
Subject: Re: Caching eval() for reuse to gain performance ?

I might be wrong here, but I think you proc method is not faster or it's
even slower than plain eval method. Every time you call a proc, eval
gets called as well.

You can try to define your proc like so:

p = eval "lambda{#{c}}"

Kent.

On 6/27/05, Neville Burnell <Neville.Burnell@bmsoft.com.au> wrote:
> Hi,
>
> I have an active record collection and I need to 'eval' a string
> against each row from the collection. Instead of processing the 'eval'

> for each row, I was thinking to 'cache' the eval into a Proc to for
reuse, ie:
>
> irb(main):001:0> @a = 1
> => 1
> irb(main):002:0> @b = 7
> => 7
> irb(main):003:0> c = %q{ @a + @b }
> => " @a + @b "
> irb(main):004:0> d = Proc.new { eval c } => #<Proc:0x02ad1240@(irb):4>

> irb(main):005:0> d.call => 8 irb(main):006:0> @a = 2 => 2
> irb(main):007:0> d.call => 9 irb(main):008:0>
>
> Is my assumption that eval is relatively expensive, and that a Proc
> call is fast correct?
> Any suggestions on how to do this better?
>
> Thanks
>
> Nev
>
>