- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
当我测试我的 java 程序时,我发现循环中的第一次运行比后面的运行花费更多的时间。
每个循环的任务是制作 5 个相同图像的缩略图并将它们存储在一个 zip 文件中。我正在使用 zip4j 和 thumbnailator。所有运行都具有相同的代码。
public static void main(String[] args) throws IOException {
try {
for(int i=0;i<10;i++){
long start = System.currentTimeMillis();
ZipFile zipFile = new ZipFile(System.nanoTime()+".zip");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
parameters.setIncludeRootFolder(false);
ArrayList<File> files = new ArrayList<File>();
for(int j=1;j<5;j++){
files.add(new File("C:\\savedFile2\\1.jpg"));
}
zipFile.createZipFile(files, parameters);
File zippedFile = zipFile.getFile();
byte[] buffer = new byte[(int)zippedFile.length()];
FileInputStream fis = new FileInputStream(zippedFile);
fis.read(buffer);
fis.close();
zippedFile.delete();
System.out.println("Time taken for "+(i+1)+"th run: "+(System.currentTimeMillis() - start));
}
} catch (ZipException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
这是我的代码。
Time taken for 1th run: 58
Time taken for 2th run: 24
Time taken for 3th run: 24
Time taken for 4th run: 24
Time taken for 5th run: 25
Time taken for 6th run: 24
Time taken for 7th run: 25
Time taken for 8th run: 25
Time taken for 9th run: 25
Time taken for 10th run: 29
从上面的结果可以看出,循环的第一次运行花费的时间是其余时间的两倍。
这里发生了什么?我怎样才能减少首次运行的时间?
最佳答案
我不认为这与循环有关,而是与 createZipFile() 函数有关,该函数似乎在第一次调用时执行一些初始化/加载。考虑以下在循环中产生相同运行时间的修改示例:
public static void main(String[] args) throws IOException {
try {
long _start = System.currentTimeMillis();
ZipFile _zipFile = new ZipFile(System.nanoTime()+".zip");
ZipParameters _parameters = new ZipParameters();
_parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
_parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
_parameters.setIncludeRootFolder(false);
ArrayList<File> _files = new ArrayList<File>();
for(int j=1;j<5;j++){
_files.add(new File("1.jpg"));
}
System.out.println("Initializing files: "+(System.currentTimeMillis() - _start));
_zipFile.createZipFile(_files, _parameters);
System.out.println("Initial run: "+(System.currentTimeMillis() - _start));
for(int i=0;i<10;i++){
long start = System.currentTimeMillis();
ZipFile zipFile = new ZipFile(System.nanoTime()+".zip");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
parameters.setIncludeRootFolder(false);
ArrayList<File> files = new ArrayList<File>();
for(int j=1;j<5;j++){
files.add(new File("1.jpg"));
}
zipFile.createZipFile(files, parameters);
File zippedFile = zipFile.getFile();
byte[] buffer = new byte[(int)zippedFile.length()];
FileInputStream fis = new FileInputStream(zippedFile);
fis.read(buffer);
fis.close();
zippedFile.delete();
System.out.println("Time taken for "+(i+1)+"tenter code hereh run: "+(System.currentTimeMillis() - start));
}
} catch (ZipException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
关于java - 如何减少循环所花费的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18502583/
我一直在读一本分配给类(class)的书,它提到数组访问需要 O(1) 时间。我意识到这非常快(也许尽可能快),但是如果您有一个循环必须多次引用它,那么分配一个临时变量以在数组中查找值有什么好处吗?或
我一直试图找出为什么这个查询花了这么长时间。以前,它的执行时间约为 150 毫秒到 200 毫秒,但现在需要 25 秒或更长时间。这是从昨晚到今天之间的事。唯一改变的就是将数据添加到表中。 根据下面的
我有一个 ng repeat 重复数据。 - data.image(src)部分为null,src=null的不再重复。 我用一个简单的 ng-if 解决了它。
我有一个包含大量测试的 Laravel 项目。我正在使用 pcov 来计算代码覆盖率,大约需要 4 分钟。但是 pcov 不支持分支覆盖,所以我决定使用 xdebug。 使用 xdebug 测试执行,
我已经被这个问题困扰了一段时间了,我被难住了。 Automapper 需要 4 秒来映射 19 个对象。在我的机器(24GB 内存,3.6Ghz i7)上,该操作应该花费毫秒或纳秒。 这是映射调用。
我有一个包含大量测试的 Laravel 项目。我正在使用 pcov 来计算代码覆盖率,大约需要 4 分钟。但是 pcov 不支持分支覆盖,所以我决定使用 xdebug。 使用 xdebug 测试执行,
我在机器 A 上有一个 java 进程通过 TCP 与机器 B 上的 Tomcat 通信。 TCP 连接(只是 syn-syn/ack 交换)始终需要 100 毫秒的数量级,而 ping 请求需要 1
我做了一项任务,从 sqlserver 获取超过 200 万条记录并将它们填充到 Asp.net GridView 中。 问题是,查询需要超过 2 分钟才能获得记录,而我的查询现在已经完全优化。 当我
我希望将 165 秒变成 2:40 而不是 0:2:45 函数需要能够适应秒值的大小。 我知道有无数种方法可以做到这一点,但我正在寻找一种干净的方法来做到这一点,除了 jQuery 之外没有任何外部库
我是一名优秀的程序员,十分优秀!