代码中的 String constants/literals 会显着降低编译速度吗?

Do String constants/literals in code slow down compiling considerably?

字符串编译时常量(内部化字符串)和文字可以与 == 进行比较,因为如果它们在某种程度上相等,则在编译时为它们分配相同的引用。

这是否意味着编译由 n 个字符串文字组成的代码需要 n log(n) 时间来编译?

我在这里问这个问题是因为有人可能已经知道答案,而且我不确定我是否可以编写一个测试来以可靠、可重现或重要的方式衡量效果。或者这个测试会反映现实世界的限制等等。

我会发布任何我能想出的测试用例,请随意提出一些建议,我会尽快实施它们。

Do String constants/literals in code slow down compiling considerably?

没有

String compile time constants (internalized-strings) and literals can be compared with the ==, as they are assigned the same reference at compile-time if they are equal somehow.

不,他们不是。它们在编译时 pooled 到 .class 文件的常量区域,并且它们 interned 在 class -加载时间,当引用被分配时。

Does this mean that compiling code consisting of n String literals takes n log(n) time to compile?

没有。这里没有固有的 O(N log(N)) 过程。在任何合理的实现中,池化是 O(1) 通过散列 table,实习同上。

[当然可能构造一个编译器或intern()方法O(N log(N)),或 O(N^3) 或更糟,但您的问题中的任何内容都不包含它。]