標題:
APCS - Java 高效讀取 (三)
[打印本頁]
作者:
tonyh
時間:
2022-12-24 19:29
標題:
APCS - Java 高效讀取 (三)
做大量輸出時,以 StringBuilder 串接好再一次輸出,取代跑多次迴圈輸出,以獲取更高的效能。更不要以 String 串接後再輸出,此方式效能最差。
以輸出 1~10000 數字為例:
迴圈輸出
StringBuilder 串接
String 串接
public class Ch100 {
long s, e;
Ch100()
{
s=System.currentTimeMillis();
//test1();
test2();
//test3();
e=System.currentTimeMillis();
System.out.println("花費: "+(e-s)+" 毫秒");
}
void test1()
{
for(int i=1; i<=10000; i++)
System.out.println(i);
System.out.println();
}
void test2()
{
StringBuilder sb=new StringBuilder();
for(int i=1; i<=10000; i++)
sb.append(i+"\n");
System.out.println(sb);
}
void test3()
{
String str="";
for(int i=1; i<=10000; i++)
str+=i+"\n";
System.out.println(str);
}
public static void main(String[] args) {
new Ch100();
}
}
複製代碼
作者:
余柏緯
時間:
2022-12-24 19:55
public class C521 {
long s,e;
C521() throws Exception
{
s=System.currentTimeMillis();
test3();
e=System.currentTimeMillis();
System.out.println("花費:"+(e-s)+" 毫秒");
}
void test1()
{
for(int i=1;i<=10000;i++)
System.out.println(i);
}
void test2()
{
StringBuilder sb=new StringBuilder();
for(int i=1;i<=10000;i++)
sb.append(i+"\n");
System.out.println(sb);
}
void test3()
{
String str=" ";
for(int i=1;i<=10000;i++)
str+=i+"\n";
System.out.println(str);
}
public static void main(String[] args) throws Exception{
new C521();
}
}
複製代碼
作者:
丁肇志
時間:
2022-12-24 19:55
public class Ch100 {
long s, e;
void Test1() {
for (int i = 1; i <= 10000; i++)
System.out.println(i);
System.out.println();
}
void Test2() {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10000; i++)
sb.append(i + "\n");
System.out.println(sb);
}
void Test3() {
String str = "";
for (int i = 1; i <= 10000; i++)
str += i + "\n";
System.out.println(str);
}
Ch100() {
s = System.currentTimeMillis();
Test1();
Test2();
Test3();
e = System.currentTimeMillis();
System.out.println("花費:" + (e-s) + "毫秒");
}
public static void main(String[] args) {
new Ch100();
}
}
複製代碼
作者:
許志捷
時間:
2022-12-24 20:01
package lol;
public class Main {
long s,e;
Main()
{
s=System.currentTimeMillis();
test1();
test2();
test3();
e=System.currentTimeMillis();
System.out.println("花費: "+(e-s)+" 毫秒");
}
void test1()
{
for(int i=1;i<=10000;i++)
{
System.out.println(i);
}
System.out.println();
}
void test2()
{
StringBuilder sb=new StringBuilder();
for(int i=1;i<=10000;i++)
sb.append(i+"\n");
System.out.println();
}
void test3()
{
String n="";
for(int i=1;i<=10000;i++)
n+=i+"\n";
System.out.println();
}
public static void main(String[] args) throws Exception{
new Main();
}
}
複製代碼
作者:
黃子倢
時間:
2022-12-24 20:02
public class P5 {
long s, e;
P5()
{
s=System.currentTimeMillis();
test2();
e=System.currentTimeMillis();
System.out.println("花費: "+(e-s)+" 毫秒");
}
void test1()
{
for(int i=1; i<=10000; i++)
System.out.println(i);
System.out.println();
}
void test2()
{
StringBuilder sb=new StringBuilder();
for(int i=1; i<=10000; i++)
sb.append(i+"\n");
System.out.println(sb);
}
void test3()
{
String str="";
for(int i=1; i<=10000; i++)
str+=i+"\n";
System.out.println(str);
}
public static void main(String[] args) {
new P5();
}
}
複製代碼
作者:
許洧熏
時間:
2022-12-24 20:02
public class Ch01 {
long s , e;
Ch01()
{
s=System.currentTimeMillis();
test2();
e=System.currentTimeMillis();
System.out.println("花費: "+(e-s)+" 毫秒");
}
void test1()
{
for(int i=1; i<=10000; i++)
System.out.println(i);
System.out.println();
}
void test2()
{
StringBuilder sb=new StringBuilder();
for(int i=1; i<=10000; i++)
sb.append(i+"\n");
System.out.println(sb);
}
void test3()
{
String str="";
for(int i=1; i<=10000; i++)
str=i+"\n";
System.out.println(str);
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
王秉鈞
時間:
2022-12-24 20:02
public class Ch49 {
long s,e;
Ch49() throws Exception
{
s=System.currentTimeMillis();
test1();
test2();
test3();
e=System.currentTimeMillis();
System.out.println("use"+(e-s)+"毫秒");
}
void test1()
{
for(int i=1;i<=10000;i++)
System.out.println(i);
System.out.println();
}
void test2()
{
StringBuilder sb=new StringBuilder();
for(int i=1;i<=10000;i++)
sb.append(i+"\n");
System.out.println(sb);
}
void test3()
{
String str="";
for(int i=1;i<=10000;i++)
str+=i+"\n";
System.out.println(str);
}
public static void main(String[] args)
{
new Ch49();
}
}
複製代碼
作者:
曾元瑜
時間:
2022-12-24 20:03
public class B3 {
long s,e;
B3(){
s=System.currentTimeMillis();
//test1();
//test2();
test3();
e=System.currentTimeMillis();
System.out.println("花費: "+(e-s)+" 毫秒");
}
void test1(){
for(int i=1;i<=10000;i++)
System.out.println(i);
System.out.println();
}
void test2(){
StringBuilder sb=new StringBuilder();
for(int i=1;i<=10000;i++)
sb.append(i+"\n");
System.out.println(sb);
}
void test3(){
String str="";
for(int i=1;i<=10000;i++)
str+=i+"\n";
System.out.println(str);
}
public static void main(String[] args) {
new B3();
}
}
複製代碼
作者:
張博竣
時間:
2022-12-24 20:04
public class Ch100 {
long s, e;
Ch100()
{
s=System.currentTimeMillis();
//test1();
test2();
//test3();
e=System.currentTimeMillis();
System.out.println("花費: "+(e-s)+" 毫秒");
}
void test1()
{
for(int i=1; i<=10000; i++)
System.out.println(i);
System.out.println();
}
void test2()
{
StringBuilder sb=new StringBuilder();
for(int i=1; i<=10000; i++)
sb.append(i+"\n");
System.out.println(sb);
}
void test3()
{
String str="";
for(int i=1; i<=10000; i++)
str+=i+"\n";
System.out.println(str);
}
public static void main(String[] args) {
new Ch100();
}
}
複製代碼
作者:
王法棣
時間:
2022-12-24 20:05
package ch02;
public class ch02 {
long a,b,c,d,e,f;
ch02()
{
a=System.currentTimeMillis();
test1();
b=System.currentTimeMillis();
System.out.println("共花費"+(b-a)+"毫秒");
}
void test1()
{
for(int i=1;i<=10000;i++)
System.out.println(i);
}
void test2()
{
StringBuilder sb=new StringBuilder();
for(int i=1;i<=10000;i++)
sb.append(i+"\n");
System.out.println(sb);
}
void test3()
{
String str="";
for(int i=1;i<=10000;i++)
str=str+i+"\n";
System.out.println(str);
}
public static void main(String[] args){
new ch02();
}
}
複製代碼
作者:
吳昀瑢
時間:
2022-12-24 20:35
public class A01 {
long a,b;
A01(){
a=System.currentTimeMillis();
test2();
b=System.currentTimeMillis();
System.out.print("花費:"+(b-a)+"毫秒");
}
void test1()
{
for(int i=1;i<=10000;i++)
{
System.out.println(i);
}
System.out.println();
}
void test2()
{
StringBuilder sb=new StringBuilder();
for(int i=1;i<=10000;i++)
{
sb.append(i+"\n");
}
System.out.println(sb);
}
void test3()
{
String str="";
for(int i=1; i<=10000; i++)
{
str+=i+"\n";
}
System.out.println(str);
}
public static void main(String[] args){
new A01();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2