arrays - ruby loop refactor -


मेरे पास एक ऐसा लूप है जो इस तरह दिखता है

  def slow_loop (array) array.each_with_index करो | आइटम, आई | Next_item = सरणी [i + 1] अगर next_item & amp; amp; Item.attribute == next_item.attribute do_something_with (next_item) अंत अंत अंत  

एक तरफ जिस तरह से do_something_with कहा जाता है बदलने से, मैं यह कैसे बेहतर प्रदर्शन कर सकता हूँ?

चूंकि ऐसा प्रतीत होता है कि यह 'ओ (एन)' ऑपरेशन है, वहां जाहिरा तौर पर यहां प्राप्त करने के लिए कोई प्रदर्शन नहीं है। , इसलिए मैंने जो जवाब चुना है वह वह है जो रूबी विधि का उपयोग करता है जो पहले से ही इस ऑपरेशन को कैप्चर करता है। आपकी मदद हर किसी के लिए धन्यवाद

जैसा कि दूसरों ने उल्लेख किया है कि आप प्रदर्शन को बेहतर बनाने में नहीं जा रहे हैं, लेकिन आप इसे और अधिक साफ तरीके से ऐसा कर सकते हैं:

  array.each_cons (2) do | a, b | Do_something_with (बी) अगर a == b end  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -