I am having the following code
def test
t1 = Time.now
@lat = params[:lat]
@lng = params[:lng]
@vehicleid = params[:vehicle_id]
@address = GoogleGeocoder.reverse_geocode([@lat, @lng]).full_address
speed = params[:speed]
speed ||= 0
Tracking.create(:latitude => @lat, :longitude => @lng, :address => @address, :vehicle_id => @vehicleid, :speed => speed)
uid = Vehicle.select('user_id').where('vehicle_id=?', @vehicleid)
gid = VehicleGeozone.where('vehicle_id=?', @vehicleid)
email = User.select('email').where('user_id=?', uid[0].user_id)
gzone = Geozone.where('geozone_id=?', gid[0].geozone_id)
@geofence = Geofence.where('geozone_id = ?', gzone[0].geozone_id)
if gzone[0].zonetype == 'polygon'
infence = false
res = isPointInPoly(@lat.to_f, @lng.to_f, @geofence)
else
res = isPointInCircle(@lat.to_f, @lng.to_f, @geofence[0].latitude, @geofence[0].longitude, @geofence[0].radius)
end
if res == true
AlertMailer.geofence(email[0].email).deliver
end
t2 = Time.now
render :text => '{"message":"success"}'
end
This test function is called every 5 seconds.
From t1 and t2 found that processing this function requires 7 seconds.
Is there a way to use threading so that I can move some intensive code of isPointInCircle
and isPointInPoly
into separate threads?