public class Jerque { // 新建類 public static void main(String[] args) { // 主方法 String str = ""; // 創(chuàng)建空字符串 // 定義對字符串執(zhí)行操作的起始時間 long starTime = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { // 利用for循環(huán)執(zhí)行10000次操作 str = str + i; // 循環(huán)追加字符串 } long endTime = System.currentTimeMillis(); // 定義對字符串操作后的時間 long time = endTime - starTime; // 計算對字符串執(zhí)行操作的時間 System.out.println("Sting消耗時間:" + time); // 將執(zhí)行的時間輸出 StringBuilder builder = new StringBuilder(""); // 創(chuàng)建字符串生成器 starTime = System.currentTimeMillis(); // 定義操作執(zhí)行前的時間 for (int j = 0; j < 10000; j++) { // 利用for循環(huán)進行操作 builder.append(j); // 循環(huán)追加字符 } endTime = System.currentTimeMillis(); // 定義操作后的時間 time = endTime - starTime; // 追加操作執(zhí)行的時間 System.out.println("StringBuilder消耗時間:" + time); // 將操作時間輸出 }}
添加回答
舉報
0/150
提交
取消