ruby: What is the most optimized expression to evaluate the same as result as with
phrase.split(delimiter).collect {|p| p.lstrip.rstrip }
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Optimised for clarity I would prefer the following:
But I presume you want to optimise for speed. I don’t know why others are speculating. The only way to find out what is faster is to benchmark your code.
Make sure you adjust the benchmark parameters – this is just an example.
Results on my laptop with the parameters listed above:
From the above I might conclude:
stripinstead of.lstrip.rstripis faster.&:strip!over{ |s| s.strip! }comes with a performance cost.split+strip.Things that I can think of that may influence the result:
But don’t take my word for it. Measure it!