String.format
Javaμμ μ¬μ© λ° λ¬Έμμ΄ μ°κ²° μ¬μ΄μ μΈμ κ°λ₯ν μ°¨μ΄κ° μμ΅λκΉ?
λλ μ¬μ©νλ κ²½ν₯μ΄ String.format
μμ§λ§ λλ‘λ λ―Έλλ¬ μ Έ μ°κ²°μ μ¬μ©ν©λλ€. νλκ° λ€λ₯Έ κ²λ³΄λ€ λ«λμ§ κΆκΈν©λλ€.
λ΄κ° 보λ λ°©μ String.format
μ λ¬Έμμ΄μ βν¬λ§·βνλ λ° λ λ§μ νμμ€λλ€. μ°κ²°μ μ€μλ‘ μ¬λΆμ % sλ₯Ό λ£κ±°λ λλ½νλ κ²μ λν΄ κ±±μ ν νμκ° μμμ μλ―Έν©λλ€.
String.format
λν 짧μ΅λλ€.
λ μ½κΈ° μ¬μ΄ κ²μ λ¨Έλ¦¬κ° μ΄λ»κ² μλνλμ§μ λ¬λ € μμ΅λλ€.
λ΅λ³
μ¬μ©νλ κ²μ΄ λ μ’μ΅λλ€ String.format()
. μ£Όλ μ΄μ λ String.format()
리μμ€ νμΌμμλ‘λ λ ν
μ€νΈλ₯Ό μ¬μ©νμ¬λ³΄λ€ μ½κ² ββμ§μν ν μ μμ§λ§ κ° μΈμ΄λ§λ€ λ€λ₯Έ μ½λλ₯Ό μ¬μ©νμ¬ μ μ€ν νμΌμ μμ±νμ§ μμΌλ©΄ μ°κ²°μ μ§μν ν μ μκΈ° λλ¬Έμ
λλ€.
μ±μ μ§μν ν μ μλλ‘ κ³ννλ κ²½μ° νμ ν ν°μ μΈμ μμΉλ₯Ό μ§μ νλ μ΅κ΄λ κ°μ ΈμΌν©λλ€.
"Hello %1$s the time is %2$t"
κ·Έλ° λ€μ νμ§ν ν μ μμΌλ©° λ€λ₯Έ μμλ₯Ό μ€λͺ νκΈ° μν΄ μ€ν νμΌμ λ€μ μ»΄νμΌνμ§ μκ³ λ μ΄λ¦ λ° μκ° ν ν°μ κ΅ν ν μ μμ΅λλ€. μΈμ μμΉλ₯Ό μ¬μ©νλ©΄ ν¨μμ λ λ² μ λ¬νμ§ μκ³ λμΌν μΈμλ₯Ό λ€μ μ¬μ©ν μλ μμ΅λλ€.
String.format("Hello %1$s, your name is %1$s and the time is %2$t", name, time)
λ΅λ³
μ±λ₯ μ 보 :
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
for(int i = 0; i < 1000000; i++){
String s = "Hi " + i + "; Hi to you " + i*2;
}
long end = System.currentTimeMillis();
System.out.println("Concatenation = " + ((end - start)) + " millisecond") ;
start = System.currentTimeMillis();
for(int i = 0; i < 1000000; i++){
String s = String.format("Hi %s; Hi to you %s",i, + i*2);
}
end = System.currentTimeMillis();
System.out.println("Format = " + ((end - start)) + " millisecond");
}
νμ΄λ° κ²°κ³Όλ λ€μκ³Ό κ°μ΅λλ€.
- μ°κ²° = 265 λ°λ¦¬ μ΄
- νμ = 4141 λ°λ¦¬ μ΄
λ°λΌμ μ°κ²°μ΄ String.formatλ³΄λ€ ν¨μ¬ λΉ λ¦ λλ€.
λ΅λ³
μ±λ₯μ λν ν λ‘ μ΄ μκΈ° λλ¬Έμ StringBuilderκ° ν¬ν¨ λ λΉκ΅λ₯Ό μΆκ° ν κ²μ΄λΌκ³ μκ°νμ΅λλ€. μ€μ λ‘ concatλ³΄λ€ λΉ λ₯΄λ©° μμ°μ€λ½κ² String.format μ΅μ μ λλ€.
μ΄κ²μ μ¬κ³Ό λ μ¬κ³Ό λΉκ΅μ μΌμ’ μΌλ‘ λ§λ€κΈ° μν΄ μΈλΆκ° μλ 루νμμ μλ‘μ΄ StringBuilderλ₯Ό μΈμ€ν΄μ€νν©λλ€ (μ€μ λ‘ λ§μ§λ§μ 루ν μΆκ°λ₯Ό μν΄ κ³΅κ°μ λ€μ ν λΉνλ μ€λ² ν€λλ‘ μΈν΄ νλμ μΈμ€ν΄μ€νλ₯Ό μννλ κ²λ³΄λ€ μ€μ λ‘ λ λΉ λ¦ λλ€) νλμ λΉλ).
String formatString = "Hi %s; Hi to you %s";
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = String.format(formatString, i, +i * 2);
}
long end = System.currentTimeMillis();
log.info("Format = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = "Hi " + i + "; Hi to you " + i * 2;
}
end = System.currentTimeMillis();
log.info("Concatenation = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
StringBuilder bldString = new StringBuilder("Hi ");
bldString.append(i).append("; Hi to you ").append(i * 2);
}
end = System.currentTimeMillis();
log.info("String Builder = " + ((end - start)) + " millisecond");
- 2012-01-11 16 : 30 : 46,058 μ 보 [TestMain]-νμ = 1416 λ°λ¦¬ μ΄
- 2012-01-11 16 : 30 : 46,190 μ 보 [TestMain]-μ°κ²° = 134 λ°λ¦¬ μ΄
- 2012-01-11 16 : 30 : 46,313 μ 보 [TestMain]-λ¬Έμμ΄ μμ±κΈ° = 117 λ°λ¦¬ μ΄
λ΅λ³
ν κ°μ§ λ¬Έμ .format
λ μ μ μ ν μμ μ μλλ€λ κ²μ
λλ€. νμμ λν μΈμκ° λ무 μ μ μ μμΌλ©° νμ μ§μ μμ λν΄ μλͺ»λ μ νμ κ°μ§ μ μμ΅λλ€. λ λ€ IllegalFormatException
λ°νμ μ λ°μνλ―λ‘ νλ‘λμ
μ μ€λ¨μν€λ λ‘κΉ
μ½λκ° μκΈΈ μ μμ΅λλ€.
λ°λλ‘, μΈμ +
λ μ»΄νμΌλ¬μμ ν
μ€νΈ ν μ μμ΅λλ€.
보μ μμ¬ μprintf( format
ν¨μκ° λͺ¨λΈλ§ λ) κΈΈκ³ λ¬΄μμ΅λλ€.
λ΅λ³
λ μ½κΈ° μ¬μ΄ κ²μ λ¨Έλ¦¬κ° μ΄λ»κ² μλνλμ§μ λ¬λ € μμ΅λλ€.
κ±°κΈ°μ λ΅μ΄ μμ΅λλ€.
κ°μΈμ μΈ μ·¨ν₯μ λ¬Έμ μ λλ€.
λ¬Έμμ΄ μ°κ²°μ΄ μ‘°κΈ λ λΉ λ₯΄λ€κ³ μκ°νμ§λ§ 무μν λ§ν©λλ€.
λ΅λ³
λ€μμ λ°λ¦¬ μ΄ λ¨μμ μ¬λ¬ μν ν¬κΈ°λ₯Ό μ¬μ©ν ν μ€νΈμ λλ€.
public class Time {
public static String sysFile = "/sys/class/camera/rear/rear_flash";
public static String cmdString = "echo %s > " + sysFile;
public static void main(String[] args) {
int i = 1;
for(int run=1; run <= 12; run++){
for(int test =1; test <= 2 ; test++){
System.out.println(
String.format("\nTEST: %s, RUN: %s, Iterations: %s",run,test,i));
test(run, i);
}
System.out.println("\n____________________________");
i = i*3;
}
}
public static void test(int run, int iterations){
long start = System.nanoTime();
for( int i=0;i<iterations; i++){
String s = "echo " + i + " > "+ sysFile;
}
long t = System.nanoTime() - start;
String r = String.format(" %-13s =%10d %s", "Concatenation",t,"nanosecond");
System.out.println(r) ;
start = System.nanoTime();
for( int i=0;i<iterations; i++){
String s = String.format(cmdString, i);
}
t = System.nanoTime() - start;
r = String.format(" %-13s =%10d %s", "Format",t,"nanosecond");
System.out.println(r);
start = System.nanoTime();
for( int i=0;i<iterations; i++){
StringBuilder b = new StringBuilder("echo ");
b.append(i).append(" > ").append(sysFile);
String s = b.toString();
}
t = System.nanoTime() - start;
r = String.format(" %-13s =%10d %s", "StringBuilder",t,"nanosecond");
System.out.println(r);
}
}
TEST: 1, RUN: 1, Iterations: 1
Concatenation = 14911 nanosecond
Format = 45026 nanosecond
StringBuilder = 3509 nanosecond
TEST: 1, RUN: 2, Iterations: 1
Concatenation = 3509 nanosecond
Format = 38594 nanosecond
StringBuilder = 3509 nanosecond
____________________________
TEST: 2, RUN: 1, Iterations: 3
Concatenation = 8479 nanosecond
Format = 94438 nanosecond
StringBuilder = 5263 nanosecond
TEST: 2, RUN: 2, Iterations: 3
Concatenation = 4970 nanosecond
Format = 92976 nanosecond
StringBuilder = 5848 nanosecond
____________________________
TEST: 3, RUN: 1, Iterations: 9
Concatenation = 11403 nanosecond
Format = 287115 nanosecond
StringBuilder = 14326 nanosecond
TEST: 3, RUN: 2, Iterations: 9
Concatenation = 12280 nanosecond
Format = 209051 nanosecond
StringBuilder = 11818 nanosecond
____________________________
TEST: 5, RUN: 1, Iterations: 81
Concatenation = 54383 nanosecond
Format = 1503113 nanosecond
StringBuilder = 40056 nanosecond
TEST: 5, RUN: 2, Iterations: 81
Concatenation = 44149 nanosecond
Format = 1264241 nanosecond
StringBuilder = 34208 nanosecond
____________________________
TEST: 6, RUN: 1, Iterations: 243
Concatenation = 76018 nanosecond
Format = 3210891 nanosecond
StringBuilder = 76603 nanosecond
TEST: 6, RUN: 2, Iterations: 243
Concatenation = 91222 nanosecond
Format = 2716773 nanosecond
StringBuilder = 73972 nanosecond
____________________________
TEST: 8, RUN: 1, Iterations: 2187
Concatenation = 527450 nanosecond
Format = 10291108 nanosecond
StringBuilder = 885027 nanosecond
TEST: 8, RUN: 2, Iterations: 2187
Concatenation = 526865 nanosecond
Format = 6294307 nanosecond
StringBuilder = 591773 nanosecond
____________________________
TEST: 10, RUN: 1, Iterations: 19683
Concatenation = 4592961 nanosecond
Format = 60114307 nanosecond
StringBuilder = 2129387 nanosecond
TEST: 10, RUN: 2, Iterations: 19683
Concatenation = 1850166 nanosecond
Format = 35940524 nanosecond
StringBuilder = 1885544 nanosecond
____________________________
TEST: 12, RUN: 1, Iterations: 177147
Concatenation = 26847286 nanosecond
Format = 126332877 nanosecond
StringBuilder = 17578914 nanosecond
TEST: 12, RUN: 2, Iterations: 177147
Concatenation = 24405056 nanosecond
Format = 129707207 nanosecond
StringBuilder = 12253840 nanosecond
λ΅λ³
λ€μ μ StringBuilder μμ toString () λ©μλλ₯Ό νΈμΆνμ¬ μμ ν μμ λμΌν ν μ€νΈ μ λλ€. μλ κ²°κ³Όλ StringBuilder μ κ·Ό λ°©μμ΄ + μ°μ°μλ₯Ό μ¬μ©νλ String μ°κ²°λ³΄λ€ μ½κ° λ립λλ€ .
νμΌ : StringTest.java
class StringTest {
public static void main(String[] args) {
String formatString = "Hi %s; Hi to you %s";
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = String.format(formatString, i, +i * 2);
}
long end = System.currentTimeMillis();
System.out.println("Format = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = "Hi " + i + "; Hi to you " + i * 2;
}
end = System.currentTimeMillis();
System.out.println("Concatenation = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
StringBuilder bldString = new StringBuilder("Hi ");
bldString.append(i).append("Hi to you ").append(i * 2).toString();
}
end = System.currentTimeMillis();
System.out.println("String Builder = " + ((end - start)) + " millisecond");
}
}
μ Έ λͺ λ Ή : (StringTestλ₯Ό 5 λ² μ»΄νμΌ λ° μ€ν)
> javac StringTest.java
> sh -c "for i in \$(seq 1 5); do echo \"Run \${i}\"; java StringTest; done"
κ²°κ³Ό :
Run 1
Format = 1290 millisecond
Concatenation = 115 millisecond
String Builder = 130 millisecond
Run 2
Format = 1265 millisecond
Concatenation = 114 millisecond
String Builder = 126 millisecond
Run 3
Format = 1303 millisecond
Concatenation = 114 millisecond
String Builder = 127 millisecond
Run 4
Format = 1297 millisecond
Concatenation = 114 millisecond
String Builder = 127 millisecond
Run 5
Format = 1270 millisecond
Concatenation = 114 millisecond
String Builder = 126 millisecond