在 ruby​​ 中哪个更快 - 哈希查找或带有 case 语句的函数? [英] Which is faster in ruby - a hash lookup or a function with a case statement?

查看:45
本文介绍了在 ruby​​ 中哪个更快 - 哈希查找或带有 case 语句的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在时间要求严格的脚本中有几个地方可以将旧 ID 转换为字符串.目前,我们在函数中使用 case 语句,如下所示:

We have a few places in a time-critical script where we convert old IDs into strings. At the moment, we use case statements inside a function, like so:

def get_name id
  case id
    when 1
      "one thing"
    when 3
      "other thing"
    else
      "default thing"
  end
end

我正在考虑用哈希查找替换它,如下所示:

I'm considering replacing this with a hash lookup, like so:

NAMES = {
  1 => "one thing",
  3 => "other thing",
}
NAMES.default = "default thing"

感觉使用 NAMES[id] 应该比使用 get_name(id) 更快——但是是吗?

It feels like it ought to be faster to use NAMES[id] than get_name(id) - but is it?

推荐答案

首先说明几点.一是像这样或多或少做同样事情的低级语言结构几乎从来都不是任何实际应用程序中的瓶颈,因此关注它们(通常)是愚蠢的.其次,正如已经提到的,如果你真的很关心它,你应该对它进行基准测试.Ruby 的基准测试和配置文件工具当然不是编程生态系统中最先进的,但它们可以完成工作.

A couple points, first. One is that low-level language constructs like this that more-or-less do the same thing are almost never the bottleneck in any real-world application, so it's (often) foolish to focus on them. Second, as has already been mentioned, if you're really concerned about it you should benchmark it. Ruby's benchmarking and profile tools are certainly not the most advanced in the programming ecosystem, but they get the job done.

我的直觉是散列会更快,因为(再次,我猜)case 语句必须依次检查每个条件(使查找项目 O(n) 而不是 O(1)).但是让我们检查一下!

My gut instinct is that hashes are going to be faster because (again, I'm guessing) the case statement must check each condition in turn (making finding the items O(n) instead of O(1)). But let's check!

完整的基准测试代码位于 https://gist.github.com/25 基本上,它生成定义适当的 case/hash 然后使用它们的文件.我继续将哈希查找放在方法调用中,这样开销就不会成为一个因素,但在现实生活中,没有理由将它卡在方法中.

Full benchmarking code is at https://gist.github.com/25 Basically, it generates a file that defines the appropriate case/hash and then uses them. I went ahead and put the hash lookup within a method call, too, so that overhead won't be a factor, but in real life there's no reason it should be stuck inside a method.

这是我得到的.在每种情况下,我都会进行 10,000 次查找.时间是以秒为单位的用户时间

Here's what I get. In each case, I'm doing 10,000 lookups. Time is user-time in seconds

Case statement, 10 items  0.020000
Hash lookup, 10 items     0.010000

Case statement, 100 items  0.100000
Hash lookup, 100 items     0.010000

Case statement, 1000 items  0.990000
Hash lookup, 1000 items     0.010000

所以,case 语句看起来很像 O(n)(这并不令人震惊).另请注意,即使在 case 语句中,10K 查找仍然只有一秒钟,因此除非您正在执行这些查找的度量标准,否则最好将重点放在代码的其余部分.

So, it looks very much like the case statement is O(n) (no shocker there). Also note that 10K lookups is still only a second even in the case statement, so unless you're doing a metric butload of these lookups, you're better off focusing on the rest of your code.

这篇关于在 ruby​​ 中哪个更快 - 哈希查找或带有 case 语句的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆