我正在研究一些需要高度优化的Java代码,因为它将在我的主程序逻辑中的许多点调用的热函数中运行.此代码的一部分涉及将double
变量乘以10
凸起到任意非负int
exponent
s.一个快速的方式(编辑:但不是最快的,见下面的更新2)获得相乘的值是switch
在exponent
:
double multiplyByPowerOfTen(final double d, final int exponent) {
switch (exponent) {
case 0:
return d;
case 1:
return d*10;
case 2:
return d*100;
// ... same pattern
case 9:
return d*1000000000;
case 10:
return d*10000000000L;
// ... same pattern with long literals
case 18:
return d*1000000000000000000L;
default:
throw new ParseException("Unhandled power of ten " + power, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
上面注释的省略号表示case
int
常量继续递增1,因此case
上面的代码片段中确实有19 秒.因为我不知道我是否真的需要10一切权力,并case
声明 …
java compiler-construction performance assembly switch-statement
使用switch
语句与使用if
30个unsigned
枚举的语句的最佳实践是什么,其中大约10个具有预期的操作(目前是相同的操作).需要考虑性能和空间,但并不重要.我已经抽象了代码片段,所以不要因为命名惯例而讨厌我.
switch
声明:
// numError is an error enumeration type, with 0 being the non-error case
// fire_special_event() is a stub method for the shared processing
switch (numError)
{
case ERROR_01 : // intentional fall-through
case ERROR_07 : // intentional fall-through
case ERROR_0A : // intentional fall-through
case ERROR_10 : // intentional fall-through
case ERROR_15 : // intentional fall-through
case ERROR_16 : // intentional fall-through
case ERROR_20 :
{
fire_special_event();
}
break;
default:
{ …
Run Code Online (Sandbox Code Playgroud) 担心我的网络应用程序的性能,我想知道哪个"if/else"或switch语句在性能方面更好?
我在java中发现很多书说switch语句比if语句更快.但我没有找到说明为什么开关比如果更快的地方.
我有一种情况我必须选择两个中的任何一项我可以使用以下任一方式
switch(item){
case BREAD:
//eat Bread
break;
default:
//leave the restaurant
}
Run Code Online (Sandbox Code Playgroud)
或使用if语句如下
if(item== BREAD){
//eat Bread
}else{
//leave the restaurant
}
Run Code Online (Sandbox Code Playgroud)
考虑项目和BREAD是常量int值
在上面的例子中,哪个更快,为什么?
在测试小字符串(例如isPhoneNumber或isHexadecimal)时,使用正则表达式会带来性能上的好处,还是会强制它们更快?不会通过检查给定字符串的字符是否在指定范围内比使用正则表达式更快来强制它们吗?
例如:
public static boolean isHexadecimal(String value)
{
if (value.startsWith("-"))
{
value = value.substring(1);
}
value = value.toLowerCase();
if (value.length() <= 2 || !value.startsWith("0x"))
{
return false;
}
for (int i = 2; i < value.length(); i++)
{
char c = value.charAt(i);
if (!(c >= '0' && c <= '9' || c >= 'a' && c <= 'f'))
{
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
与
Regex.match(/0x[0-9a-f]+/, "0x123fa") // returns true if regex matches whole given expression
Run Code Online (Sandbox Code Playgroud)
似乎有一些与正则表达式相关的开销,即使模式是预编译的,只是因为正则表达式必须在许多一般情况下工作.相比之下,蛮力方法完全符合要求而不再需要.我错过了正则表达式的一些优化吗?
我正在阅读有关C编程的本教程.它说:
switch语句实际上完全不同(来自其他语言),实际上是一个"跳转表".您可以只放置导致整数的表达式,而不是随机布尔表达式,这些整数用于计算从开关顶部到匹配该值的部分的跳转.这里有一些代码我们将分解以理解"跳转表"的概念.
但是,需要比较switch语句的情况,直到找到匹配项(否则返回default).
它与多个if-else语句有什么不同呢?或者,它只是一个语法糖?我错过了一些重要的东西吗?
我正试图检查某个char
元音是否是元音.这样做的最佳方法是什么?
我想做这样的事情:
int i = 0;
switch(difficulty) {
case 1: i++; break;
case 2: i--; break;
default: case 1;
}
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?我想防止重复的代码.我知道在这个特定的例子中没有理由这样做,因为重复的代码会很小.我能想出的唯一一件事是[使用开关盒的能力下降]:
switch(difficulty) {
case 2: i--; break;
default:
case 1: i++; break;
}
Run Code Online (Sandbox Code Playgroud)
我宁愿不这样做,因为增加案例数量并在底部有默认值会更有意义.
但是我想知道,如果我这样做,它会搞砸引擎盖下的goto语句吗?特别是,不需要更长的时间来决定使用哪个goto语句,因为数字或乱序?订单在switch语句中是否重要?想象一下,所有案例都有相同的被调用几率,如果你以随机顺序而不是线性顺序将它们放在一起会有意义吗?
[编辑:对于我关于效率的问题,我发现:交换语句的顺序是否重要,简短的答案是否:交换机案例顺序是否会影响速度? Java的交换机是如何工作的?
java ×6
if-statement ×3
performance ×3
c++ ×2
assembly ×1
brute-force ×1
c ×1
char ×1
optimization ×1
regex ×1
string ×1