与命令行相比,为什么我的应用程序在 IntelliJ 中运行得更快?

2022-01-20 00:00:00 performance ide java

我们有一个应用程序,它通过拆分数据并对其进行排序来导入大量文件.运行 JUnit 测试用例时,整个过程大约需要 16 分钟.

We have an application that imports a large amount of files by splitting the data and sorting it. When running the JUnit test case, the whole process takes about 16 minutes.

同样的测试,使用 mvn clean test -Dtest=MyTest 在 34 分钟 内完成.

Same test, done with mvn clean test -Dtest=MyTest run in 34 minutes.

我们正在调用 /bin/sort 来对文件进行排序.排序似乎需要更长的时间.我不明白有什么不同.

We are calling in to /bin/sort to sort the files. The sort seems to be taking longer. I don't understand what is different.

看看 IntelliJ 它运行与

Looking at IntelliJ it runs with

/Library/Java/JavaVirtualMachines/1.6.0_26-b03-383.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 -Didea.launcher.bin.path=/Applications/IntelliJ IDEA 10.app/bin -Dfile.encoding=UTF-8 -classpath %classhpath% com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 xxx.IntTestImportProcess,testImportProcess

我在 OS X 上.所有的类都是使用 Spring 注入的.关于 IntelliJ 性能提升背后的理论,有哪些可能的建议?测试是相同的.我不能分享所有的代码,因为太多了.但如果需要,我可以添加任何细节.

I am on OS X. All the classes are injected using Spring. What are some possible suggestions are theories at what is behind this performance gain in IntelliJ? The tests are identical. I can't share all of the code because there is just so much. But I can add any detail if requested.

这是我的主要课程以及我如何运行这两个课程.

Here is my main class and how I am running both.

public static void main(String... args) throws IOException {
        if(args.length != 2) {
            System.out.println("Usage: 
  java -jar client.jar spring.xml data_file");
            System.exit(1);
        }
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext(args[0]);
        PeriodFormatter formatter = new PeriodFormatterBuilder()
                .appendMinutes()
                .appendSuffix("minute", "minutes")
                .appendSeparator(" and ")
                .appendSeconds()
                .appendSuffix("second", "seconds")
                .toFormatter();
        URI output = (URI) applicationContext.getBean("workingDirectory");
        File dir = new File(output);
        if(dir.exists()) {
            Files.deleteDirectoryContents(dir.getCanonicalFile());
        }
        else {
            dir.mkdirs();
        }
        ImportProcess importProcess = applicationContext.getBean(ImportProcess.class);
        long start = System.currentTimeMillis();
        File file = new File(args[1]);
        importProcess.beginImport(file);
        Period period = new Period(System.currentTimeMillis() - start); // in milliseconds
        System.out.println(formatter.print(period.toPeriod()));
    }

我决定删除 JUnit,只使用 main() 方法.结果完全一样.IntelliJ 又来了.这是疯狂的日志.

I have decided to remove JUnit and just use a main() method. The result are exactly the same. IntelliJ is again. Here is the crazy log.

使用 IntelliJ

With IntelliJ

DEBUG [ main] 2011-08-18 13:05:16,259 [er.DelimitedTextUnixDataSorter] Sorting file [/Users/amirraminfar/Desktop/import-process/usage]
DEBUG [ main] 2011-08-18 13:06:09,546 [er.DelimitedTextUnixDataSorter] Sorting file [/Users/amirraminfar/Desktop/import-process/customer]

使用 java -jar

With java -jar

DEBUG [ main] 2011-08-18 12:10:16,726 [er.DelimitedTextUnixDataSorter] Sorting file [/Users/amirraminfar/Desktop/import-process/usage]
DEBUG [ main] 2011-08-18 12:15:55,893 [er.DelimitedTextUnixDataSorter] Sorting file [/Users/amirraminfar/Desktop/import-process/customer]

排序命令是

sort -t'    ' -f -k32,32f -k18,18f -k1,1n

如上所示,在 Intellij 中排序需要 1 分钟,但在 java -jar 中需要 5 分钟!

As you can see above, sorting in Intellij take 1 minutes but in java -jar takes 5 minutes!

更新

我使用 /Library/Java/JavaVirtualMachines/1.6.0_26-b03-383.jdk/Contents/Home/bin/java 运行所有内容,但排序仍然需要 5 多分钟.

I ran everything using /Library/Java/JavaVirtualMachines/1.6.0_26-b03-383.jdk/Contents/Home/bin/java and the sorting still takes well over 5+ mins.

推荐答案

感谢大家的帮助.事实证明 IntelliJ 以 LANG=C 开始排序.Mac OS X 终端默认以 UTF8 排序,这解释了性能损失.希望这个答案会对某人有所帮助.

Thank you everybody for helping. It turns out IntelliJ starts sort with LANG=C. Mac OS X terminal sorts by default in UTF8 which explains the performance loss. Hopefully this answer will help somebody.

相关文章