์นดํ…Œ๊ณ ๋ฆฌ ๋ณด๊ด€๋ฌผ: Java

Java

Java์˜ String Concatenation๋ณด๋‹ค String.format์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๊นŒ? ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝํ–ฅ์ด

String.formatJava์—์„œ ์‚ฌ์šฉ ๋ฐ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ์‚ฌ์ด์— ์ธ์‹ ๊ฐ€๋Šฅํ•œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ?

๋‚˜๋Š” ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝํ–ฅ์ด 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 ๋Ÿฐํƒ€์ž„ ์— ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ํ”„๋กœ๋•์…˜์„ ์ค‘๋‹จ์‹œํ‚ค๋Š” ๋กœ๊น… ์ฝ”๋“œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜๋Œ€๋กœ, ์ธ์ˆ˜ +๋Š” ์ปดํŒŒ์ผ๋Ÿฌ์—์„œ ํ…Œ์ŠคํŠธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ณด์•ˆ ์—ญ์‚ฌ ์˜( 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