有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

StringBuilder如何大幅减少Java程序所需的时间?

有两个Java代码片段。在下面分享它们-

一,

Collections.sort(al);
                Iterator<Integer> it = al.iterator();
                while(it.hasNext()){
                        sb.append(it.next());
                        sb.append("\n");
                }
                System.out.println(sb.toString());

二,

Collections.sort(al);
                Iterator<Integer> it = al.iterator();
                while(it.hasNext()){
                        System.out.println(it.next());
                }

分享上述计划所花费的时间- 1.1.43秒 2.4.28秒

我想知道magic StringBuilder是做什么的,有人可以指导吗


共 (1) 个答案

  1. # 1 楼答案

    这不是您的StringBuilder,而是您的系统。出来println()语句,这会减慢执行时间

    它很慢,因为

    Bytes had to be sent to the console application -> Each char has to be rendered using a true type font (cause for slow processing) -> Displayed area may have to be scrolled to append a new line to the visible area.