νƒœκ·Έ 보관물: concatenation

concatenation

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