title: ImageIO.read(...) - It is very slow, is there a better way?
body: <p>I am loading tons of icons that are going to be used in my application. I plan to load all of them from the jar on server startup. However, with hundreds of images adding up to just over 9MB, it is still taking over 30 seconds to perform this task. I am doing it in a separate thread right now but it is making me wonder if I am doing something inefficiently in my code. I borrowed code from SO to load the information into my structures. I put the code into a test class and profiled it. 99% of the profile is on the ImageIO.read(..) method. So that is definitely the bottleneck. Below is that test class that should provide a picture as to how I am using ImageIO.</p>
  
  <pre><code>public class IconTest {
  
  /**
   * @param args the command line arguments
   * @throws java.net.URISyntaxException
   * @throws java.io.IOException
   */
  public static void main(String[] args) throws URISyntaxException, IOException {
      URI uri = IconTest.class.getResource("Icons").toURI();
      Path myPath;
      if (uri.getScheme().equals("jar")) {
          FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.&lt;String, Object&gt;emptyMap());
          myPath = fileSystem.getPath("Icons/");
      } else {
          myPath = Paths.get(uri);
      }
      IconFolder root = new IconFolder(myPath.toFile().getName());
      IconFolder parentFolder = root;
      HIcon currentIcon = null;
      IconFolder folder = null;
      HashMap&lt;String,IconFolder&gt; folders = new HashMap&lt;&gt;();
      folders.put(parentFolder.getName(), parentFolder);
      Stream&lt;Path&gt; walk = Files.walk(myPath, 5);
      Iterator&lt;Path&gt; it = walk.iterator();it.next();
      while(it.hasNext()){
          Path path = it.next();
          if(path.toFile().isDirectory()){
              folder = new IconFolder(path.toFile().getName());
              folders.put(folder.getName(), folder);
              String parentName = path.getParent().toFile().getName();
              parentFolder = folders.get(parentName);
              parentFolder.addSubFolder(folder);
              currentIcon =null;
              System.out.println("Directory: " + path);
          }else{
              URL url = path.toUri().toURL();
  
              ImageIcon icon = new ImageIcon(ImageIO.read(url));
              //Image image = Toolkit.getDefaultToolkit().getImage(url);
              //ImageIcon icon = new ImageIcon(image);
              String[] iconName;
              iconName = path.getFileName().toString().replaceAll("_000000", "").replaceAll(".png","").split("_",2);
              String imageName = iconName[0];
              String imageSize = iconName[1];
              if(currentIcon==null||!currentIcon.getName().equals(imageName)){
                  currentIcon = new HIcon(imageName);
                  folder.addIcon(currentIcon);
                  currentIcon.setIcon(icon, imageSize );
              }else{
                  currentIcon.setIcon(icon, imageSize);
              }
              //System.out.println("Image: " + imageName+"--&gt;"+imageSize);
          }
      }
      System.out.println("");
  }
  }
  </code></pre>
  
  <p>Any pointers would be helpful. I looked at a number of the SO posts that I think are pointing out the same thing. I am using a MacBook Air with SSD so I would think this would be lightning fast. </p>
  
  <p>I added a screenshot of the profile results below:</p>
  
  <p><a href="https://i.stack.imgur.com/6805U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6805U.png" alt="Java profile in netbeans"></a></p>
  
  <p>This is a profile after setting <code>setUseCache</code> to false:</p>
  
  <p><a href="https://i.stack.imgur.com/DDgho.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DDgho.png" alt="Java profile after setUseCache = false"></a></p>