为什么在ide中运行.jar要比运行相同的.java文件花费2-3倍的时间?
复活节期间,我制作了一个程序,它将获取一个大的日志文件,读入并解析它,然后用javafx将其输出到一个表视图。该程序工作正常,因此使用Intellij将其制作成一个.jar,这样就可以在没有IDE的情况下轻松使用 然而,当以JAR形式运行I/O方法时,运行程序所需的时间大约是运行程序的2-3倍 (.java文件)3.5秒读入->(.jar文件)11.3秒 (.java文件)0.014秒写入->(.jar文件)0.034秒为什么在ide中运行.jar要比运行相同的.java文件花费2-3倍的时间?,java,performance,jar,executable-jar,Java,Performance,Jar,Executable Jar,复活节期间,我制作了一个程序,它将获取一个大的日志文件,读入并解析它,然后用javafx将其输出到一个表视图。该程序工作正常,因此使用Intellij将其制作成一个.jar,这样就可以在没有IDE的情况下轻松使用 然而,当以JAR形式运行I/O方法时,运行程序所需的时间大约是运行程序的2-3倍 (.java文件)3.5秒读入->(.jar文件)11.3秒 (.java文件)0.014秒写入->(.jar文件)0.034秒 /** * Method that filters a file bas
/**
* Method that filters a file based on a search string and a selected radio button
*
* @param num index of a selected radio button
* @param filterSearch String that is being searched for
* @param path Of the log file that is being filtered through
* @return The new file that contains only the desired lines
*/
public static String filterFile(int num, String filterSearch, String path) {
long startTime = System.currentTimeMillis();
ArrayList<String> toOutput = new ArrayList<>();
BufferedReader reader = null;
File inputFile = new File(path);
try {
reader = new BufferedReader(new FileReader(inputFile));
String searchString = null;
//While the log file has a line
//the next line to be searched is stored in searchString
while ((searchString = reader.readLine())!=null) {
switch (num) {
//if the user radio button is selected
case 1:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("user=\"" + filterSearch)) {
toOutput.add(searchString);
}
break;
case 2:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("srcip=\"" + filterSearch)) {
toOutput.add(searchString);
}
break;
}
}
} catch (IOException e) {
System.out.println("Cannot open file: ");
System.exit(0);
}finally {
try{
reader.close();
}catch (IOException ef){
}
}
long endTime = System.currentTimeMillis();
double duration = (endTime - startTime)/1000.0;
System.out.println("first formatedSeconds = "+ duration);
return createFilterFile(toOutput);
}
我已经阅读了有关这方面的老问题。我已经尝试过对这些方法进行计时,确保使用最新的JDK/JRE
我敢肯定,作为jar运行时,这些方法的速度会减慢:
/**
* Method that filters a file based on a search string and a selected radio button
*
* @param num index of a selected radio button
* @param filterSearch String that is being searched for
* @param path Of the log file that is being filtered through
* @return The new file that contains only the desired lines
*/
public static String filterFile(int num, String filterSearch, String path) {
long startTime = System.currentTimeMillis();
ArrayList<String> toOutput = new ArrayList<>();
Scanner in = null;
File inputFile = new File(path);
try {
in = new Scanner(inputFile);
String searchString = null;
//While the log file has a line
while (in.hasNext()) {
//the next line to be searched is stored in searchString
searchString = in.nextLine();
switch (num) {
//if the user radio button is selected
case 1:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("user=\"" + filterSearch)) {
//System.out.println(searchString);
toOutput.add(searchString);
}
break;
case 2:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("srcip=\"" + filterSearch)) {
toOutput.add(searchString);
}
break;
}
}
} catch (Exception e) {
System.out.println("Cannot open file: ");
System.exit(0);
}finally {
in.close();
}
long endTime = System.currentTimeMillis();
double duration = (endTime - startTime)/1000.0;
System.out.println("first formatedSeconds = "+ duration);
return createFilterFile(toOutput);
}
/**
* Create a new file with the filtered results inside
* @param toPrint lines from the log file that have been filtered out
* @return Path to this new file
*/
private static String createFilterFile(ArrayList<String> toPrint){
long startTime = System.currentTimeMillis();
Date date = new Date();
String fileName = "New filtered search file " + sdf.format(date) + ".log";
PrintWriter out = null;
try{
FileWriter outputFile = new FileWriter(fileName, true);
out = new PrintWriter(outputFile);
for(String item: toPrint){
out.println(item);
}
}catch(Exception e){
e.printStackTrace();
}finally {
out.close();
}
long endTime = System.currentTimeMillis();
double duration = (endTime - startTime)/1000.0;
// formatedSeconds = (0.xy seconds)
System.out.println("second formatedSeconds = "+ duration);
return fileName;
}
/**
*方法,该方法基于搜索字符串和选定单选按钮筛选文件
*
*@param num所选单选按钮的索引
*@param filterSearch正在搜索的字符串
*正在筛选的日志文件的@param路径
*@返回仅包含所需行的新文件
*/
公共静态字符串筛选器文件(int num、字符串筛选器搜索、字符串路径){
long startTime=System.currentTimeMillis();
ArrayList toOutput=新的ArrayList();
扫描仪输入=空;
文件输入文件=新文件(路径);
试一试{
in=新扫描仪(输入文件);
字符串searchString=null;
//而日志文件有一行
while(在.hasNext()中){
//要搜索的下一行存储在searchString中
searchString=in.nextLine();
开关(num){
//如果选择了用户单选按钮
案例1:
//如果searchString包含筛选器搜索
//将整行打印到新文件中
if(searchString.contains(“用户=\”“+filterSearch)){
//System.out.println(搜索字符串);
添加(搜索字符串);
}
打破
案例2:
//如果searchString包含筛选器搜索
//将整行打印到新文件中
if(searchString.contains(“srcip=\”“+filterSearch)){
添加(搜索字符串);
}
打破
}
}
}捕获(例外e){
System.out.println(“无法打开文件:”);
系统出口(0);
}最后{
in.close();
}
long-endTime=System.currentTimeMillis();
双持续时间=(结束时间-开始时间)/1000.0;
System.out.println(“第一个格式化的秒数=+持续时间”);
返回createFilterFile(toOutput);
}
/**
*创建一个包含筛选结果的新文件
*@param toPrint已从日志文件中筛选出的行
*@返回此新文件的路径
*/
私有静态字符串createFilterFile(ArrayList toPrint){
long startTime=System.currentTimeMillis();
日期=新日期();
String fileName=“新筛选的搜索文件”+sdf.format(date)+“.log”;
PrintWriter out=null;
试一试{
FileWriter outputFile=新的FileWriter(文件名,true);
out=新的PrintWriter(输出文件);
用于(字符串项:toPrint){
out.println(项目);
}
}捕获(例外e){
e、 printStackTrace();
}最后{
out.close();
}
long-endTime=System.currentTimeMillis();
双持续时间=(结束时间-开始时间)/1000.0;
//FormattedSeconds=(0.xy秒)
System.out.println(“第二个格式化秒数=+持续时间”);
返回文件名;
}
Edit在这两种方法中都添加了finally子句,没有改变任何性能
这段代码获取大日志文件,在每行中搜索搜索字符串并将其添加到数组列表中,然后创建一个新文件。此文件要小得多,因此可以轻松打开
任何对提高jar性能的帮助都将不胜感激,因为我对.jar文件没有太多经验
编辑:从扫描仪更改为缓冲读卡器大大提高了性能
(.java文件)1.39秒读入->(.jar文件)1.82秒
(.java文件)0.014秒写入->(.jar文件)0.034秒
/**
* Method that filters a file based on a search string and a selected radio button
*
* @param num index of a selected radio button
* @param filterSearch String that is being searched for
* @param path Of the log file that is being filtered through
* @return The new file that contains only the desired lines
*/
public static String filterFile(int num, String filterSearch, String path) {
long startTime = System.currentTimeMillis();
ArrayList<String> toOutput = new ArrayList<>();
BufferedReader reader = null;
File inputFile = new File(path);
try {
reader = new BufferedReader(new FileReader(inputFile));
String searchString = null;
//While the log file has a line
//the next line to be searched is stored in searchString
while ((searchString = reader.readLine())!=null) {
switch (num) {
//if the user radio button is selected
case 1:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("user=\"" + filterSearch)) {
toOutput.add(searchString);
}
break;
case 2:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("srcip=\"" + filterSearch)) {
toOutput.add(searchString);
}
break;
}
}
} catch (IOException e) {
System.out.println("Cannot open file: ");
System.exit(0);
}finally {
try{
reader.close();
}catch (IOException ef){
}
}
long endTime = System.currentTimeMillis();
double duration = (endTime - startTime)/1000.0;
System.out.println("first formatedSeconds = "+ duration);
return createFilterFile(toOutput);
}
/**
*方法,该方法基于搜索字符串和选定单选按钮筛选文件
*
*@param num所选单选按钮的索引
*@param filterSearch正在搜索的字符串
*正在筛选的日志文件的@param路径
*@返回仅包含所需行的新文件
*/
公共静态字符串筛选器文件(int num、字符串筛选器搜索、字符串路径){
long startTime=System.currentTimeMillis();
ArrayList toOutput=新的ArrayList();
BufferedReader reader=null;
文件输入文件=新文件(路径);
试一试{
reader=newbufferedreader(newfilereader(inputFile));
字符串searchString=null;
//而日志文件有一行
//要搜索的下一行存储在searchString中
而((searchString=reader.readLine())!=null){
开关(num){
//如果选择了用户单选按钮
案例1:
//如果searchString包含筛选器搜索
//将整行打印到新文件中
if(searchString.contains(“用户=\”“+filterSearch)){
添加(搜索字符串);
}
打破
案例2:
//如果searchString包含筛选器搜索
//将整行打印到新文件中
if(searchString.contains(“srcip=\”“+filterSearch)){
添加(搜索字符串);
}