7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

newとnewInstanceのパフォーマンス

Last updated at Posted at 2013-08-08

実装について同僚と話していて気になったので調べた。
http://d.hatena.ne.jp/ihiroky/20090827/1251388837
を参考にさせてもらってちょっとプラス調べたことを書き留めておく。

実行環境は自分のmac, java7
詳しいスペックとか面倒臭いのでとりあえず割愛

キャッシュなし
long start0 = System.nanoTime();
String.class.newInstance();
long time0 = System.nanoTime() - start0;
System.out.println("newInstance() : " + time0);

long start1 = System.nanoTime();
new String();
long time1 = System.nanoTime() - start1;
System.out.println("new           : " + time1);
newInstance() : 117000
new           : 3000
キャッシュあり
String.class.newInstance();
long start0 = System.nanoTime();
String.class.newInstance();
long time0 = System.nanoTime() - start0;
System.out.println("newInstance() : " + time0);

new String();
long start1 = System.nanoTime();
new String();
long time1 = System.nanoTime() - start1;
System.out.println("new           : " + time1);
newInstance() : 4000
new           : 1000
キャッシュあり,if文分岐1つ目
String.class.newInstance();
long start0 = System.nanoTime();
String.class.newInstance();
long time0 = System.nanoTime() - start0;
System.out.println("newInstance() : " + time0);

String str = new String();
Class clazz = str.getClass();
long start1 = System.nanoTime();
if (clazz == String.class) {
           new String();
}
long time1 = System.nanoTime() - start1;
System.out.println("new           : " + time1);
newInstance() : 4000
new           : 1000
キャッシュあり,if文分岐2つ目
String.class.newInstance();
long start0 = System.nanoTime();
String.class.newInstance();
long time0 = System.nanoTime() - start0;
System.out.println("newInstance() : " + time0);

String str = new String();
Class clazz = str.getClass();
long start1 = System.nanoTime();
if (clazz == Integer.class) {
           new String();
} else if (clazz == String.class) {
           new String();
}
long time1 = System.nanoTime() - start1;
System.out.println("new           : " + time1);
newInstance() : 4000
new           : 31000
キャッシュあり,if文分岐3つ目
String.class.newInstance();
long start0 = System.nanoTime();
String.class.newInstance();
long time0 = System.nanoTime() - start0;
System.out.println("newInstance() : " + time0);

String str = new String();
Class clazz = str.getClass();
long start1 = System.nanoTime();
if (clazz == Integer.class) {
           new String();
} else if (clazz == Long.class) {
           new String();
} else if (clazz == String.class) {
           new String();
}
long time1 = System.nanoTime() - start1;
System.out.println("new           : " + time1);
newInstance() : 4000
new           : 56000

if文での比較にclassを使ってたりするのは今回自分が調べたい対象だったからなので
ツッコミはなしでお願いします。

結論

検証方法がかなり荒いので話半分ですが、
if文で制御してやるならnewInstance()でもパフォーマンスに関して問題ない感じ

追記

@asahina_dev さんにコメントで教えてもらった

String.class.getConstructor().newInstance();

についても確認してみた

キャッシュなし
                 long start2 = System.nanoTime();
                 String.class.getConstructor().newInstance();
                 long time2 = System.nanoTime() - start2;
                 System.out.println("getConstructor() : " + time2);

getConstructor() : 148000
キャッシュあり
                 String.class.getConstructor().newInstance();
                 long start2 = System.nanoTime();
                 String.class.getConstructor().newInstance();
                 long time2 = System.nanoTime() - start2;
                 System.out.println("getConstructor() : " + time2);
getConstructor() : 13000

getInstance()よりgetConstructor()の方が遅い理由の一つは
getConstructorの場合は継承メンバーまで含むからなのかなーと思っている

7
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?