[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfriends-ml 10709] Re: Java Performance Tuning (2nd Edition)



  高橋(智)です。

"TAKAHASHI, Tomohiro" wrote:
>
>   [Java Performance Tuning (2nd Edition)]
>     http://www.amazon.co.jp/exec/obidos/ASIN/0596003773
>     http://www.amazon.com/exec/obidos/tg/detail/-/0596003773
snip

  この本をようやく読み始めたのですが、Chapter6 の Exceptions の箇所に
以下の議事録に関係するTopicが有りました。

-- リファクタリングを読む会第 6回議事録 より --
> P316
>  try〜catch を使うとパフォーマンスが悪くなる?
>     -->try自体はそれほどでもないが、catchにジャンプするととてもパフォーマンス
>        が悪くなる
>     -->頻繁に catch に来るようであれば、対策が必要では?

  以下、適当に引用いたします。
        (※なお、この他に
             The Cost of try-catch Blocks with an Exception
             Assertion Overhead
         というTopicもあります)
=======================================================================
The Cost of try-catch Blocks Without an Exception
    try-catch blocks generally use no extra time if no exception is thrown,
  although some VMs may impose a slight penalty. The following test
  determines whether a VM imposes any significant overhead for try-catch
  blocks when the catch block is not entered. The test runs the same code
  twice, once with the try-catch entered for every loop iteration and
  again with just one try-catch wrapping the loop. Because we're testing
  the VM and not the compiler, you must ensure that your compiler has not
  optimized the test away; use an old JDK version to compile it if necessary.
  To determine that the test has not been optimized away by the compiler,
  you need to compile the code, then `decompile' it:
-----------------------------------
public class TryCatchTimeTest {
  public static void main(String[] args) {
    int REPEAT = 10000000;
    if (args.length == 1)
      REPEAT = Integer.parseInt(args[0]);
    System.out.println("Using a repeat value (number of loop iterations) of " +
                                                                       REPEAT);
    ...
    ...
  }

  public static boolean try_catch_not_in_loop(int repeat, Object[] o) {
    Integer i[] = new Integer[3];
    try {
      for (int j = repeat; j > 0; j--) {
        i[0] = (Integer) o[(j+1)%2];
        i[1] = (Integer) o[j%2];
        i[2] = (Integer) o[(j+2)%2];
      }
      return false;
    }
    catch (Exception e) {
      return true;
    }
  }

  public static boolean try_catch_in_loop(int repeat, Object[] o) {
    Integer i[] = new Integer[3];
    for (int j = repeat; j > 0; j--) {
      try {
        i[0] = (Integer) o[(j+1)%2];
        i[1] = (Integer) o[j%2];
        i[2] = (Integer) o[(j+2)%2];
      }
      catch (Exception e) {
        return true;
      }
    }
    return false;
  }
}
-----------------------------------

    Running this test in various VMs results in increases in the time
  taken by the looped tyr-catch test relative to the nonlooped test
  for some VMs; however, the latest VMs show penalty. See Table 6-1.

Table6-1.  Extra cost of the looped try-catch test relative to
           the nonlooped try-catch test
┏──────────────────────────────────┓
│VM               |1.1.8|1.2.2│1.3.1│1.3.1  │1.4.0│1.4.0  │1.4.0│
│                 |     |     │     │-server│     │-server│-Xint│
│──────────────────────────────────│
│Increase in time |〜5% |〜10%| None │ None  │None │ None  │〜2% │
┗──────────────────────────────────┛
=======================================================================

-- 
高橋智宏
  Java読書会( http://www.t3-jpn.com/bof/ )
  T3-Japan( http://www.t3-jpn.com/ )