考虑到你有这样的代码:
doSomething() // this method may throw a checked a exception
//do some assignements calculations
doAnotherThing() //this method may also throw the same type of checked exception
//more calls to methods and calculations, all throwing the same kind of exceptions.
Run Code Online (Sandbox Code Playgroud)
现在我知道,实际上在构造异常时会出现性能损失,特别是展开堆栈.我还阅读了几篇文章,指出在输入try/catch块时会有轻微的性能损失,但这些文章似乎都没有结论.
我的问题是,是否建议将try catch中的行保持最小?,即只在try子句中包含可以实际抛出正在捕获的异常的行.try子句中的代码运行速度较慢或导致性能下降吗?
但考虑到这一点,更重要的是最佳实践/更易读的解决方案:
try {
doSomething() // this method may throw a checked a exception
//do some assignements calculations
doAnotherThing() //this method may also throw the same type of checked exception
//more calls to methods and calculations, all throwing …Run Code Online (Sandbox Code Playgroud) 我知道进入一个catch块在执行一个程序时有一些显着的成本,但是,我想知道是否进入try {}块也有任何影响因此我开始在google中寻找一个有很多意见的答案,但没有基准测试所有.我发现的一些答案是:
然而他们并没有用事实回答我的问题,所以我决定自己尝试一下.
这就是我做的.我有一个这种格式的csv文件:
host;ip;number;date;status;email;uid;name;lastname;promo_code;
状态之后的所有内容都是可选的,甚至没有相应的; ,所以在解析验证时必须要查看值是否存在,这就是我想到的try/catch问题.
我在公司里继承的当前代码是这样的:
StringTokenizer st=new StringTokenizer(line,";");
String host = st.nextToken();
String ip = st.nextToken();
String number = st.nextToken();
String date = st.nextToken();
String status = st.nextToken();
String email = "";
try{
email = st.nextToken();
}catch(NoSuchElementException e){
email = "";
}
Run Code Online (Sandbox Code Playgroud)
它重复了用uid,name,lastname和promo_code为电子邮件做的事情.
我改变了一切:
if(st.hasMoreTokens()){
email = st.nextToken();
}
Run Code Online (Sandbox Code Playgroud)
事实上它表现得更快.解析没有可选列的文件时.以下是平均时间:
--- Trying:122 milliseconds
--- Checking:33 milliseconds
Run Code Online (Sandbox Code Playgroud)
然而,这就是让我感到困惑的原因和我要问的原因:当在CSV的所有8000行中运行带有可选列值的示例时,if()版本仍然比try/catch版本表现更好,所以我的问题是
try块是否真的对我的代码没有任何性能影响?
此示例的平均时间为:
--- Trying:105 milliseconds
--- Checking:43 milliseconds
Run Code Online (Sandbox Code Playgroud)
有人能解释一下这里发生了什么吗?
非常感谢
捕获异常而不是进行检查时,try-catch需要多长时间(以纳秒为单位)(假设消息具有HashMap类型的查找性能)?
try {
timestamp = message.getLongField( MessageField.TIMESTAMP );
} catch (MissingDataException e) {
//Not all messages contain this field
}
Run Code Online (Sandbox Code Playgroud)
VS
if (message.contains(MessageField.TIMESTAMP))
timestamp = message.getLongField( MessageField.TIMESTAMP );
Run Code Online (Sandbox Code Playgroud) 所以我正在尝试制作一个简单的计算器.
当我输入第一个数字时,如何进行,但是如果我插入"abc",它将给我一个错误.
当你写"abc"说"请输入一个数字"时,我如何按顺序制作
import java.util.Scanner;
public class calculator
{
public static void main(String[] args0) {
Scanner test = new Scanner(System.in);
int x;
int y;
String c;
System.out.println("Insert a number ");
x = test.nextInt();
System.out.println("insert a value e.g * / + -");
c = test.next();
System.out.println("Insert another number");
y = test.nextInt();
if ( c.equals("*")) {
System.out.println("the total is " + x*y);
}
if (c.equals("+")) {
System.out.println("the total is " + (x+y));
}
if (c.equals("-")) {
System.out.println("the total is "+ (x-y));
} …Run Code Online (Sandbox Code Playgroud)