Skip to content

Instantly share code, notes, and snippets.

10.times do
r = Restaurant.find(:all, :conditions => ['id IN (?)', ids])
r.map(&:cuisines).flatten.map(&:id)
end
# benchmarked 10 times
user system total real
10.170000 0.470000 10.640000 ( 11.809886)
9.630000 0.410000 10.040000 ( 11.076710)
11.220000 0.520000 11.740000 ( 12.819519)
10.times do
r = Restaurant.find(:all, :include => [:cuisines], :conditions => ['id IN (?)', ids])
r.map(&:cuisines).flatten.map(&:id)
end
# benchmarked 10 times
user system total real
5.540000 0.010000 5.550000 ( 5.553716)
5.560000 0.000000 5.560000 ( 5.566566)
5.670000 0.010000 5.680000 ( 5.687871)
r.map(&:cuisines).flatten.map(&:object_id).uniq.length
=> 2001
r.map(&:cuisines).flatten.map(&:id).uniq.length
=> 73
def Restaurant.add_cuisines(restaurants)
return [] if restaurants.empty?
ids = []
id_map = {}
restaurants.each do |r|
id_map[r[:id]] = r
ids << r[:id]
# make sure ActiveRecord won't try to load cuisines again
r.cuisines.loaded
10.times do
r = Restaurant.find(:all, :conditions => ['id IN (?)', ids])
Restaurant.add_cuisines(r)
r.map(&:cuisines).flatten.map(&:id)
end
# benchmarked 10 times
user system total real
3.070000 0.040000 3.110000 ( 3.148139)
3.040000 0.030000 3.070000 ( 3.133394)
# NoSession acts like a session, but doesn't get stored anywhere. Sneaky.
class NoSession < AbstractSession
class << self
def find_session(session_id); end
def create_session(session_id, data={})
new(session_id, data)
end
def delete_all(condition=nil); end
end
before_filter :toggle_session
def toggle_session
# Disable the session if we spy a bot (or it was
# disabled elsewhere), enable otherwise.
if @disable_session.nil?
@disable_session = UrbanspoonUtil.is_megatron?(request.user_agent)
end
OptionalSessionStore.disabled = @disable_session
end
class OptionalSessionStore < SqlSessionStore
cattr_accessor :enabled_session_class
cattr_accessor :disabled
class << self
def disabled=(v)
if self.disabled != v
@@disabled = v
self.toggle_session_class
(use '(incanter core charts io latex))
(defn stirling [#^Integer n]
(* (Math/sqrt (* 2 n Math/PI))
(Math/pow n n)
(Math/pow Math/E (* -1 n))))
(defn fact [#^Integer n]
(reduce * (range 2 (inc n))))
user> (fact 200)
788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000
user> (class (fact 200))
java.math.BigInteger
user> (stirling 200)
Infinity
user> (class (stirling 200))
java.lang.Double