简体   繁体   中英

How to increase the performance of java code?

I want to ask how to increase code performance? I need to take all the html code and save it in a Queue-LinkedList. But in the extraction process, I use the loop inside the loop O (n ^ 2). Which is too slow. How to improve this code?

public class ParsingHtml {

private static Queue<Character> queueCharacter = new LinkedList<>();

public static void downloadHtmlCode(String addressUrl) throws IOException {

    InputStream is = null;
    try (BufferedReader bufferedReader =
                 new BufferedReader(new InputStreamReader(is = (new URL(addressUrl)).openStream()))) {
        bufferedReader.lines()
                .filter(str -> !str.isEmpty())
                .forEach(str -> {
                    for (char ch : str.toCharArray())
                        if (ch != ' ') queueCharacter.add(ch);
                });
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    is.close();
   }
}

Your foreach is doing map and collect operations.

Replace conversion of strings to char array via map function and the queue.add by collect terminate function.

-->

lines().flatmap(String::chars).mapToObj(c -> (char) c).filter(c -> c.= ' ').collect(Collections:toCollection(LinkedList:;new));

You can use flatmap as below,

bufferedReader.lines()
            .filter(str -> !str.isEmpty())
            .flatMap(str->str.chars().mapToObj(x -> (char) x))
            .filter(ch->ch != ' ')
            .collect(Collectors.toCollection(LinkedList::new));

Comlexity here is not O(n^2) . In your code you read each symbol twice, not n^2 times. First read is when you read line, second is when you iterate over symbols in that line. It means comlexity is О(n) . You can do same thing in one read: just read html character-by-character and put that characters in Queue while reading.

private static Queue<Character> queueCharacter = new LinkedList<>();

public static void main(String[] args) {

    try (InputStream inputStream = new URL(addressUrl).openStream()) {
        BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream));
        int c = 0;
        while ((c = buffer.read()) != -1) {
            char character = (char) c;
            if (character != ' ' && character != '\n') {
                //filter space and endline symbol
                queueCharacter.add(character);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM