对于不同的编译版本和不同的机器,std :: hash对于相同的输入是否给出相同的结果? [英] Does std::hash give same result for same input for different compiled builds and different machines?

查看:204
本文介绍了对于不同的编译版本和不同的机器,std :: hash对于相同的输入是否给出相同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些随机测试参数,我需要为其计算哈希值以检测是否以相同的参数运行。我可能会使用在不同时间重新编译的相同源代码或在另一台计算机上运行该测试。

I have some random test parameters for which I need to calculate a hash to detect if I ran with same parameters. I might run the test using the same source recompiled at a different time or run on a different machine.

即使如此,我也想检测是否对相同的参数使用了相同的参数。跑。
对于不同的编译版本和不同的机器, std :: hash 对于相同的输入是否给出相同的结果?

Even so I want to detect whether the same parameters were used for the run. Does std::hash give the same result for the same input for different compiled builds and different machines?

例如

std::hash<string>{}("TestcaseParamVal0.7Param0.4");

这将永远是一个唯一的数字吗?

Will this always be a unique number?

推荐答案

否, std :: hash 不能保证结果在计算机,构建甚至执行过程中都相同在同一台计算机上的同一内部版本。您唯一的保证是,在一次执行期间,相等的对象具有相同的哈希值。 (当然,不能保证不相等的对象具有不同的哈希值。)

No, std::hash does not guarantee that the result will be the same across computers, builds, or even executions of the same build on the same computer. Your only guarantee is that during one execution, objects that are equal have the same hash. (There is, of course, no guarantee that objects that are unequal have different hashes.)

某些实现会在执行之间更改哈希结果,但这样做并不可行。在存在具有相同散列的多个键的情况下,可减轻由于散列表的性能差而导致的拒绝服务风险。该标准明确允许这样做,它仅保证结果在程序运行期间是一致的。

Some implementations go out of their way to change hash results between executions, as it mitigates denial of service risks due to the poor performance of hash tables in the presence of many keys with the same hash. This is explicitly allowed by the standard, which only guarantees that results are consistent for the duration of the program.

如果您需要执行与机器之间的可重复性,则不能使用 std :: hash ,并且必须推出自己的等效项。

If you need repeatability between executions and machines, you can't use std::hash and must roll out your own equivalent.

这篇关于对于不同的编译版本和不同的机器,std :: hash对于相同的输入是否给出相同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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