I have two different methods which actually do the same but are implemented a bit different. They walk through a directory and read all files in it and check how many files with a certain name are in the directory. Now I want to know which is faster but both are similar and take around 3-4 seconds (the directory has millions of files) but how can I know which is really faster? Is there a method which compares the speed of them?
method)
private void getAllRelatedFilesEig(String corrId) throws InterruptedException, IOException { log.debug("Get all files with corrId=" + corrId + " from directory=" + processingDir); Profiler profiler = Profiler.createStarted(); Files.list(Paths.get(processingDir)) .filter(p -> p.getFileName().toString() .indexOf("EPX_" + corrId + "_") >= 0) .forEach( path -> { try { EPEXFile file = new EPEXFile(path); if (file.isTranMessage()) { if (file.isOrderMessage()) { orderFiles.add(file); } else { tradeFiles.add(file); } } else { infoFiles.add(file); } } catch (IFException ex) { log.error("Error creating EPEXFile object " + ex.getMessage()); } } ); profiler.stop("allFilesWithSameCorrIdRetrieval"); log.info(orderFiles.size() + " order files with corrId=" + corrId); log.info(tradeFiles.size() + " trade files with corrId=" + corrId); log.info(infoFiles.size() + " info files with corrId=" + corrId); profiler = Profiler.createStarted(); profiler.stop("processFiles"); orderFiles.clear(); tradeFiles.clear(); infoFiles.clear(); }
method)
private void getAllRelatedFilesOrig(String corrId) throws InterruptedException, IOException { log.debug("Get all files with corrId=" + corrId + " from directory=" + processingDir); Path dirPath = Paths.get(processingDir); ArrayList<Path> fileList; Profiler profiler = Profiler.createStarted(); try (Stream<Path> paths = Files.walk(dirPath)) { fileList = paths.filter(t -> (t.getFileName().toString().indexOf("EPX_" + corrId + "_") >= 0)) .collect(Collectors.toCollection(ArrayList::new)); for (Path path : fileList) { try { EPEXFile file = new EPEXFile(path); if (file.isTranMessage()) { if (file.isOrderMessage()) { orderFiles.add(file); } else { tradeFiles.add(file); } } else { infoFiles.add(file); } } catch (IFException ex) { log.error("Error creating EPEXFile object " + ex.getMessage()); } } } profiler.stop("allFilesWithSameCorrIdRetrieval"); log.info(orderFiles.size() + " order files with corrId=" + corrId); log.info(tradeFiles.size() + " trade files with corrId=" + corrId); log.info(infoFiles.size() + " info files with corrId=" + corrId); profiler = Profiler.createStarted(); profiler.stop("processFiles"); orderFiles.clear(); tradeFiles.clear(); infoFiles.clear(); }
I have tried to figure it out with the Profiler class but I could not figure out which is exactly faster since sometimes the first and sometimes the second is faster. Is there even a way to say which is faster in general? Even when it is just a little bit faster it would help me to know which one it is.