我有以下代碼優化方法
def calculate_metrics
reach_values = {}
reach_values['reach_change_value'] = 0
reach_values['reach_change_type'] = 'nochange'
if find_yesterdays_metrics
change = (get_influencer_metrics["total_count"]).to_i - find_yesterdays_metrics.reach_count
reach_values['reach_change_value'] = change.abs
case
when change < 0
reach_values['reach_change_type'] = "down"
when change > 0
reach_values['reach_change_type'] = "up"
end
end
reach_values
end
def set_metrics_data
@facebook_metrics = @influencer_metrics.new(
reach_change_value: calculate_metrics['reach_change_value'],
reach_change_type: calculate_metrics['reach_change_type'],
social_account: SocialAccount.friendly.find('facebook')
)
end
的calculate_metrics
被調用了兩次,現在正在和完全處理兩次。有沒有關於如何優化相同的建議,以便我不必處理相同的數據兩次。
作爲從快看,在set_metrtics_data爲什麼不把calculate_metrics在tmp變量x中的結果,然後詢問x:calculate_metrics [y]和x:calculate_metrics [z]? –
ahh有道理:)將試試:) –
讓我知道結果:) –