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