TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Fast Ruby – A collection of common Ruby idioms

111 点作者 daviducolo将近 10 年前

15 条评论

eregon将近 10 年前
I would advise against concluding anything with < 20% gain, the changes often impacts readability (the intention becomes less clear) and might as well be measurement errors or just be insignificant for any sort of real application. Not to mention, of course, these measurements are specific to a given system, implementation, etc.
评论 #10009596 未加载
评论 #10009134 未加载
评论 #10010002 未加载
评论 #10009766 未加载
diminish将近 10 年前
Shouldn&#x27;t <i>some</i> of those items be opened as a ticket to Ruby 2.x and standard library to ask them for a faster implementation?
评论 #10008718 未加载
comboy将近 10 年前
Really nice project, and there&#x27;s a lot to learn from it.<p>But I think if your choice is ruby then you are putting clean code above micro optimizations. And it often pays off.
评论 #10008976 未加载
评论 #10008975 未加载
xutopia将近 10 年前
Choose readability over speed unless speed is a problem.
tboyd47将近 10 年前
I like to visualize code quality as a point on a triangle with corners labeled &quot;readable&quot;, &quot;extensible&quot;, and &quot;performant.&quot; Most of what is considered &quot;low-quality&quot; code is biased towards one side of the triangle, and most code quality advice tends towards over-correcting that bias towards another corner of the triangle.<p>What I love about this presentation is that it&#x27;s not <i>advice</i> -- it&#x27;s a collection of tools for your perusal. Use at your own discretion. A lot of quote-unquote &quot;high-quality&quot; Ruby code I&#x27;ve seen is either biased towards readability, biased towards extensibility, or sitting somewhere on the edge between the two. So I really do think every Ruby developer should at least glance at this collection and be familiar with it.
seivadmas将近 10 年前
If you are writing Ruby you already decided you didn&#x27;t care about speed.<p>Don&#x27;t get me wrong, I think Ruby is a great language and I use it every day to get paid, but it is not a speed queen. Ruby&#x27;s strengths lie in flexibility, fast iteration, readable code and permitting a functional style.<p>In most practical web applications the big bottlenecks will be either view rendering or database. Choosing to put any focus at all on performance of something like parallel vs serial assignment (unless you are doing something truly pathological) is a complete waste of time.<p>It will have no noticeable difference to the end user and distracts from the far more important job of making your code modular, extensible and readable.<p>If need your code to be fast and you are running Ruby, you already lost. Use Java or a compiled language instead.
tsewlliw将近 10 年前
This is neat! I think the &quot;why&quot; section thats on some of them is the most valuable part, absorbing that &quot;why&quot; into your lizard brain can add up to huge changes in your natural style :)
waxjar将近 10 年前
In situations where it&#x27;s unlikely calling a method will result in a NoMethodError, i prefer rescuing the exception over checking if the method exists first. This will be faster when it does not result in a NoMethodError.<p>In the &quot;Enumerable#select.last vs Enumerable#reverse.detect&quot; benchmark it would be interesting to know what the result of Enumerable#reverse_each.detect would be.
geoffharcourt将近 10 年前
I&#x27;m surprised at the speed difference between parallel and sequential assignment styles. I prefer the sequential, so that&#x27;s a nice bonus that it&#x27;s also more performant.
评论 #10014022 未加载
decentrality将近 10 年前
Awesome comparisons. Extremely good to know. Would love Rubinius on Linux, and comma delimited or underscore delimited results. Maybe I&#x27;ll run an post those that way.
bradleyland将近 10 年前
This is a great collection, but any listing of microbenchmarks needs a caveat.<p>Consider the first example, parallel assignment vs sequential assignment. As we can see by the results, parallel assignment is 2.25x slower, which seems like a monumental impact to performance, right? If all your application does is assign a few variables and exit, sure, but very few applications are this simplistic. In order to make a good judgement call on this optimization, you have to understand the impact within the context of your application:<p>What is the total execution time of your application?<p>What portion of that execution time is spent on assignment?<p>What portion of that execution time is spent on the extra allocation of an array due to parallel assignment?<p>At the bottom of the benchmark, we can see the iteration rate for each. Parallel assignment managed a rate of 2521708.9 iterations per second. We can work out the total execution time per iteration from this number:<p>Single iteration as a fraction of a second: 1&#x2F;2521708.9<p>In decimal form: 0.000000396556478 s<p>Converted to milliseconds: 0.000397 ms<p>The same conversion for for parallel assignment gets us: 0.0001758783 ms.<p>In each iteration, we save 0.0002206782 ms.<p>Circling back to my list of questions, what is the total execution time of my application? If my app uses an I&#x2F;O calls — and especially network I&#x2F;O — it could be hundreds of ms. At this delta, it would take over 4,500 iterations of this optimization to achieve an improvement of 1 ms. If we&#x27;re talking about an operation that occurs locally and is 100% in-memory, execution times may be &lt;50 ms, at which point, you&#x27;d need around 2,250 iterations to get a 1 ms improvement.<p>At this point, I have to tattle on myself. This is an obtuse method of analysis. Microbenchmarks are hard, and at the i&#x2F;s rate we&#x27;re seeing here, there could be confounding factors that the author (and I) haven&#x27;t accounted for. Things like garbage collection and object caching will have an impact at these time scales. Also, we have to ask whether our microbenchmark reflects reality? What real world application repeatedly assigns literals to variables millions of times per second? Extrapolating any meaningful decisions from the microbenchmarks alone is a fools errand.<p>The lesson is that microbenchmarks can only tell you so much. A comprehensive approach to optimization involves looking at the total run time and apportionment of time in an actual application. This process is called profiling, and the tools for profiling Ruby applications have improved in recent times.<p>Looking at the parallel vs sequential assignment difference, what you really want to know is whether parallel vs sequential assignment is impacting <i>your</i> application, and to what degree. Profiling tools will tell you where your application spends its time, and where it&#x27;s allocating memory. This tells you where to look. Microbenchmarks will tell you which idioms you pay a penalty for. The combination of the two allows you to make smart decisions.<p>If you have a a parallel assignment wrapped in a loop that will execute hundreds of thousands of times every time your application runs, this will show up during profiling. Moving to sequential optimization will likely pay dividends. Otherwise, the penalty paid for parallel assignment is probably minimal. Profiling is a good way to tell the difference.
meneses将近 10 年前
Good job. It would be nice if benchmarks could be arranged by the gains in ascending order.
muP将近 10 年前
Is there a similar collection for other languages, like Python?
supergeek133将近 10 年前
Super interesting... thanks!
robertpohl将近 10 年前
Good read!