简体   繁体   English

Java是否可以在大写的文字字符串常量上跳过.toUpperCase()?

[英]Can Java skip .toUpperCase() on literal string constants already in upper case?

I have a .toUpperCase() happening in a tight loop and have profiled and shown it is impacting application performance. 我有一个.toUpperCase()发生在紧密的循环中,并且进行了剖析并显示出它正在影响应用程序性能。 Annoying thing is it's being called on strings already in capital letters. 令人讨厌的是,它已经被用大写字母的字符串调用了。 I'm considering just dropping the call to .toUpperCase() but this makes my code less safe for future use. 我正在考虑仅删除对.toUpperCase()的调用,但这会使我的代码日后使用的安全性降低。

This level of Java performance optimization is past my experience thus far. 到目前为止,这种水平的Java性能优化是我的经验。 Is there any way to do a pre-compilation, set an annotation, etc. to skip the call to toUpperCase on already upper case strings? 有什么方法可以进行预编译,设置注释等,以跳过对已经大写的字符串的toUpperCase调用吗?

What you need to do if you can is call .toUpperCase() on the string once, and store it so that when you go through the loop you won't have to do it each time. 如果可以的话,您需要做的是在字符串上调用一次.toUpperCase()并将其存储起来,这样当您遍历循环时就不必每次都这样做了。

I don't believe there is a pre-compilation situation - you can't know in advance what data the code will be handling. 我不相信会有预编译的情况-您无法预先知道代码将处理什么数据。 If anyone can correct me on this, it's be pretty awesome. 如果有人可以纠正我的问题,那就太棒了。

If you post some example code, I'd be able to help a lot more - it really depends on what kind of access you have to the data before you get to the loop. 如果您发布一些示例代码,我将能够提供更多帮助-这实际上取决于您进入循环之前对数据的访问类型。 If your loop is actually doing the data access (eg, reading from a file) and you don't have control over where those files come from, your hands are a lot more tied than if the data is hardcoded. 如果您的循环实际上是在进行数据访问(例如,从文件中读取),并且您无法控制这些文件的来源,那么与对数据进行硬编码相比,您的双手会更加束缚。

Any many cases there's an easy answer, but in some, there's not much you can do. 在任何情况下,都有一个简单的答案,但是在某些情况下,您无能为力。

You can try equalsIgnoreCase , too. 您也可以尝试equalsIgnoreCase It doesn't make a new string. 它不会产生新的字符串。

No you cannot do this using an annotation or pre-compilation because your input is given during the runtime and the annotation and pre-compilation are compile time constructions. 不,您不能使用注释或预编译来执行此操作,因为您的输入是在运行时给出的,并且注释和预编译是编译时的构造。

If you would have known the input in advance then you could simply convert it to uppercase before running the application, ie before you compile your application. 如果您事先知道输入,则可以在运行应用程序之前(即在编译应用程序之前)将其转换为大写形式。

Note that there are many ways to optimize string handling but without more information we cannot give you any tailor made solution. 请注意,有许多方法可以优化字符串处理,但是如果没有更多信息,我们将无法为您提供任何量身定制的解决方案。

You can write a simple function isUpperCase(String) and call it before calling toUpperCase(): 您可以编写一个简单的函数isUpperCase(String)并在调用toUpperCase()之前调用它:

if (!isUpperCase(s)) {
    s = s.toUpperCase()  
}

It might be not significantly faster but at least this way less garbage will be created. 它可能不会明显更快,但是至少以这种方式,将创建更少的垃圾。 If a majority of the strings in your input are already upper case this is very valid optimization. 如果输入中的大多数字符串已经是大写字母,则这是非常有效的优化。

isUpperCase function will look roughly like this: isUpperCase函数将大致如下所示:

boolean isUpperCase(String s) {
    for (int i = 0; i < s.length; i++) {
        if (Character.isLowerCase(s.charAt(i)) {
            return false;
        }
    }
    return true;
}

you need to do an if statement that conditions those letters out of it. 您需要执行一个if语句,以限制其中的那些字母。 the ideas good just have a condition. 好主意只有条件。 Then work with ascii codes so convert it using (int) then find the ascii numbers for uppercase which i have no idea what it is, and then continue saying if ascii whatever is true then ignore this section or if its for specific letters in a line then ignore it for charAt(i) 然后使用ascii代码,因此使用(int)对其进行转换,然后找到我不知道它是什么的大写字母的ascii数字,然后继续说ascii是否为真,然后忽略此部分或是否针对一行中的特定字母然后忽略它的charAt(i)

sorry its a rough explanation 对不起,粗略的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM