Apologies, I have been out of using ruby for a while, and ran into
trouble when I tried to do the below. What is the normal way to call
apply a list of arguments to a list of functions?
Mixing f(unction) and m(ethod) terminology a bit… but that’s OK,
because it will also work if f1 and f2 are Proc objects (which are
basically anonymous functions.
If you have strings or symbols, you can use them to get the method
objects (see above), or you can use send:
Am I correct if I remember that using send is quite expensive in
cpu-time? Especially when I am using it millions of times… $pop is
about 5000 hosts large.
Ah. It’s not that bad. send(“fib”,2) is about twice as slow as fib(2).
So I lose 2 minutes in 2 hours of simulation, which is fine.
Am I correct if I remember that using send is quite expensive in
cpu-time? Especially when I am using it millions of times… $pop is
about 5000 hosts large.
10000.times {
# Events to happen to every host this 0.1 year:
[“birth”,“death”,“infect”,“evolve”].shuffle.each {|event|
next if event == "birth" and EVENTS_BIRTH > rand
next if event == "death" and EVENTS_DEATH > rand
next if event == "infect" and EVENTS_INFECTION > rand
next if event == "evolve" and EVENTS_EVOLUTION > rand
$pop.each {|host|
next if host == nil
send(event,host)
}
}
}
What I could do is make birth,death,infect and evolve methods of the
host class, and perhaps use call then. Does that make sense?
Mixing f(unction) and m(ethod) terminology a bit… but that’s OK,
because it will also work if f1 and f2 are Proc objects (which are
basically anonymous functions.
If you have strings or symbols, you can use them to get the method
objects (see above), or you can use send:
Am I correct if I remember that using send is quite expensive in
cpu-time? Especially when I am using it millions of times… $pop is
about 5000 hosts large.
10000.times {
# Events to happen to every host this 0.1 year:
[“birth”,“death”,“infect”,“evolve”].shuffle.each {|event|
Using #send with a String argument is slower than using it with a
Symbol. Benchmark:
On Sun, 08 Feb 2009 15:56:04 -0500, Boris S. wrote:
Boris S. wrote:
Am I correct if I remember that using send is quite expensive in
cpu-time? Especially when I am using it millions of times… $pop is
about 5000 hosts large.
Ah. It’s not that bad. send(“fib”,2) is about twice as slow as fib(2).
So I lose 2 minutes in 2 hours of simulation, which is fine.
Thanks for the answers!
It should be a bit faster if you use :fib instead of “fib”, because the
ruby runtime has to internally “fib” to :fib every time you call send().
I’m taking a stab in the dark here but are you trying to pass more than one
procedures as arguments to a function?
if so, then try:
def foo(x, y)
x.call
y.call
end
foo proc { puts ‘x’ }, proc { puts ‘y’ }
Anyway i could spend hours trying to understand that question.