在 Grails 和 Groovy 中,字符串(单引号)是否优于 GString?
In Grails and Groovy, do Strings (in single quotes) outperform GStrings?
由于在 Grails 和 Groovy 中,单引号字符串与 GStrings 不同 class(双引号字符串允许 ${variable} 值注入),使用单引号是否更有效,除了在使用 ${variables} 时?我猜测双引号的存在需要解析普通单引号字符串不需要的字符串。因此,我希望额外的时间寻找 ${} 的存在会对性能造成非常轻微的影响。
因此,一般来说,制定鼓励使用单引号的约定似乎是有益的,除非使用双引号有特定优势。我错了吗?
看看Double Quoted String下的第一个注释。
Double quoted strings are plain java.lang.String
if there’s no
interpolated expression, but are groovy.lang.GString
instances if
interpolation is present.
这是可能的,因为插值是延迟检测的,因此确定是将其作为 GString 还是 String 引用。例如尝试以下断言:
assert 'sample'.class.simpleName == 'String'
assert "sample".class.simpleName == 'String'
assert "sample ${1 != 2}".class.simpleName == 'GStringImpl'
上面的第二个断言清楚地表明双引号默认是 String
,直到有一个插值(如第三个断言)。
因此,只要不存在插值,使用单引号还是双引号都没有关系。但是,我个人认为在使用 String
或 GString
.
的规模上不必担心性能优势。
is it more efficient to use single quotes, except when ${variables}
are being used?
没有。对于没有使用 ${variables}
的场景,它们的表现是完全一样的。如果双引号字符串中没有 ${variables}
,则系统不会创建 GString
。它创建了一个标准 java.lang.String
.
编辑以解决以下评论中发布的单独问题:
它发生在编译时。代码如下:
void someMethod(String a) {
def s1 = "some string"
def s2 = "some string with arg ${a}"
}
@groovy.transform.CompileStatic
void someOtherMethod(String a) {
def s1 = "some string"
def s2 = "some string with arg ${a}"
}
编译为:
public void someMethod(String a)
{
CallSite[] arrayOfCallSite = $getCallSiteArray();
Object s1 = "some string";
Object s2 = new GStringImpl(new Object[] { a }, new String[] { "some string with arg ", "" });
}
public void someOtherMethod(String a)
{
String s1 = "some string";
GString s2 = new GStringImpl(new Object[] { a }, new String[] { "some string with arg ", "" });
}
由于在 Grails 和 Groovy 中,单引号字符串与 GStrings 不同 class(双引号字符串允许 ${variable} 值注入),使用单引号是否更有效,除了在使用 ${variables} 时?我猜测双引号的存在需要解析普通单引号字符串不需要的字符串。因此,我希望额外的时间寻找 ${} 的存在会对性能造成非常轻微的影响。
因此,一般来说,制定鼓励使用单引号的约定似乎是有益的,除非使用双引号有特定优势。我错了吗?
看看Double Quoted String下的第一个注释。
Double quoted strings are plain
java.lang.String
if there’s no interpolated expression, but aregroovy.lang.GString
instances if interpolation is present.
这是可能的,因为插值是延迟检测的,因此确定是将其作为 GString 还是 String 引用。例如尝试以下断言:
assert 'sample'.class.simpleName == 'String'
assert "sample".class.simpleName == 'String'
assert "sample ${1 != 2}".class.simpleName == 'GStringImpl'
上面的第二个断言清楚地表明双引号默认是 String
,直到有一个插值(如第三个断言)。
因此,只要不存在插值,使用单引号还是双引号都没有关系。但是,我个人认为在使用 String
或 GString
.
is it more efficient to use single quotes, except when ${variables} are being used?
没有。对于没有使用 ${variables}
的场景,它们的表现是完全一样的。如果双引号字符串中没有 ${variables}
,则系统不会创建 GString
。它创建了一个标准 java.lang.String
.
编辑以解决以下评论中发布的单独问题:
它发生在编译时。代码如下:
void someMethod(String a) {
def s1 = "some string"
def s2 = "some string with arg ${a}"
}
@groovy.transform.CompileStatic
void someOtherMethod(String a) {
def s1 = "some string"
def s2 = "some string with arg ${a}"
}
编译为:
public void someMethod(String a)
{
CallSite[] arrayOfCallSite = $getCallSiteArray();
Object s1 = "some string";
Object s2 = new GStringImpl(new Object[] { a }, new String[] { "some string with arg ", "" });
}
public void someOtherMethod(String a)
{
String s1 = "some string";
GString s2 = new GStringImpl(new Object[] { a }, new String[] { "some string with arg ", "" });
}