此代碼應該在文件夾中查找圖像,並將圖像合併到6000x6000圖像中。它正在工作,但它真的很慢。我可以實現的任何優化?有沒有什麼辦法可以讓我優化這個java代碼?
File in = new File(args[1]);
File out = new File(args[2]);
in.mkdirs();
out.mkdirs();
if(out.exists())
{
out.delete();
}
if(!in.isDirectory())
{
Main.printUsage();
}
BufferedImage bout = new BufferedImage(6032, 6032, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bout.createGraphics();
int count = 0;
long starttime = System.currentTimeMillis();
for(int i=0; i<=376; i++)
{
for(int k=0; k<=376; k++)
{
File cu = new File(in, (i-188)+"-"+(k-188)+".png");
if(cu.exists())
{
count++;
try {
g.drawImage(ImageIO.read(cu), 16*i, 16*k, null);
} catch (IOException e) {
e.printStackTrace();
}
Runtime.getRuntime().;
}
}
}
System.out.println("Processed "+count+" chunks in "+((System.currentTimeMillis()-starttime)/1000F)+" seconds");
g.dispose();
try {
ImageIO.write(bout, "png", out);
} catch (IOException e) {
e.printStackTrace();
}
其中*部分*緩慢結合對圖像的?時間到了哪裏?簡單地通過時間戳添加日誌記錄可能很好地隔離哪個位是問題。 –
我很抱歉,自編譯以來,我修改了源代碼。什麼都不應該改變 – HansiHE
對於已經工作的代碼的優化,你應該考慮http://codereview.stackexchange.com –