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