ページ

2014年4月1日

あれからJavaのリフレクションのスピードはどうなったのか?

僕が一番最近Javaのリフレクションのパフォーマンスを計測したのは、今のアリエルの開発を始めたぐらいで、ライブラリの選定をしている時でした。jdkのバージョンも覚えていません。それより前はいつやったのか覚えていません。
その時の結果の詳細は残っていませんが、リフレクションは通常のアクセスより10倍以上遅い、って印象だけが残っています。印象でものを語るのは良くないし、バージョンが上がるに連れて改善されているのかもしれないので、もう一度計測してみました。

以前は自分でコードを書いていたのですが、今回はサボって http://www.ibm.com/developerworks/jp/java/library/j-dyn0603/ にあるコードで試します。このページには1.4での計測結果ものっていて、いいかも。

ちなみに、今回はLinux上のSunのJDKでコンパイル・ビルド・実行しています。それから、結果に乗っている最初の一回目の数値は無視してください。

まず、リフレクションでオブジェクトを作るときの結果です。

Java version 1.7.0_51
Java HotSpot(TM) 64-Bit Server VM
24.51-b03
Oracle Corporation

Direct Object creation:
 49 1 2 1 2
 average time = 2 ms.
Reflection Object creation:
 44 3 3 2 3
 average time = 3 ms.
Direct byte[8] creation:
 10 7 8 11 10
 average time = 9 ms.
Reflection byte[8] creation:
 12 8 8 8 8
 average time = 8 ms.
Direct byte[64] creation:
 101 79 37 8 7
 average time = 33 ms.
Reflection byte[64] creation:
 7 7 7 8 8
 average time = 8 ms.

Direct Object creation:
 2 2 1 3 1
 average time = 2 ms.
Reflection Object creation:
 2 4 5 4 4
 average time = 4 ms.
Direct byte[8] creation:
 9 6 7 8 8
 average time = 7 ms.
Reflection byte[8] creation:
 8 8 10 9 8
 average time = 9 ms.
Direct byte[64] creation:
 9 8 11 9 7
 average time = 9 ms.
Reflection byte[64] creation:
 9 11 11 10 8
 average time = 10 ms.

オブジェクトの作成は若干リフレクションの方が遅いですが、使用用途にもよりますが、この程度であればリフレクションを使ってもそれほどオーバーヘッドもなく、問題ないでしょう。


次にオブジェクトの中のフィールドへのアクセスです。昔、偉い人が「privateなんて飾りです。リフレクションをシラン人はわからんのです。」って言っていましたが、それがこういうことです。
で、結果ですが、


Java version 1.7.0_51
Java HotSpot(TM) 64-Bit Server VM
24.51-b03
Oracle Corporation

Direct access using member field:
 8 6 6 7 6
 average time = 6 ms.
Reference access to member field:
 28 7 7 5 7
 average time = 7 ms.
Reflection access to member field:
 3544 3486 3437 3433 3482
 average time = 3460 ms.

Direct access using member field:
 7 5 7 6 6
 average time = 6 ms.
Reference access to member field:
 6 6 6 6 6
 average time = 6 ms.
Reflection access to member field:
 3538 3555 3515 3545 3482
 average time = 3524 ms.

で、reference accessはまあ、どうでもいいです。reflection accessだと、10倍どころじゃないですね。600倍ぐらい遅いです。数回だけならまだしも、アクセス数が増えると使い物にならないですね。JRubyとかJPythonは、基本的にメンバー変数に直接アクセスはしないんだろうか?

最後にメソッドの呼び出しです。

Java version 1.7.0_51
Java HotSpot(TM) 64-Bit Server VM
24.51-b03
Oracle Corporation

Direct call using member field:
 8 7 6 6 3
 average time = 6 ms.
Direct call using passed value:
 11 6 3 6 7
 average time = 6 ms.
Call to object using member field:
 10 6 7 7 6
 average time = 7 ms.
Call to object using passed value:
 8 6 5 7 6
 average time = 6 ms.
Reflection call using member field:
 71 32 30 29 33
 average time = 31 ms.
Reflection call using passed value:
 158 139 138 122 117
 average time = 129 ms.

引数なしでの呼び出しは、通常の5倍遅いです。引数ありでは20倍の遅さです。メンバー変数へ直接アクセスすするよりは、メソッドを通してアクセスするほうが効率は良さそうですね。

IBMのサイトの結果と比べると…、IBMのサイトのグラフが対数になっていて、よくわかりません。多分、あんまり変わっていないです。

ということで、

- リフレクションでのオブジェクトの作成はオーバーヘッドがほとんどない
- メンバーフィールドにアクセスするのは遅すぎるのでやらないほうがいい。
- メソッド呼び出しは5から20倍遅いので、使う場合は注意する

22 件のコメント:

for IT the さんのコメント...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

sheela さんのコメント...

Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
Best Devops online Training
Online DevOps Certification Course - Gangboard

sasitamil さんのコメント...


It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Selenium training in bangalore

Ada Smith さんのコメント...

My favorite casino! purely top online casino I recommend to everyone!!! The engine does not slow down. I did not observe any breaks in the connection. Won $ 2200. Brought the winnings to no problem. Very beautiful, pleasant, comfortable casino. Very nice voice acting. Honesty control. In short .. all zashib

service care さんのコメント...

I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating us.
redmi service center near me
mi service center

Unknown さんのコメント...

Click here to get daily info more.

Chris Hemsworth さんのコメント...

The article is so informative. This is more helpful. Thanks for sharing

Learn best software testing online certification course class in chennai with placement
Best selenium testing online course training in chennai
Best online software testing training course institute in chennai with placement
.

Madhu Balan さんのコメント...

All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
Java Training in Chennai
Java Training in Coimbatore
Java Training in Bangalore

chandhran m さんのコメント...

I learned more information from this blog.the concepts are explained in detailed.
Aviation Academy in Chennai
Air Hostess Training in Chennai
Airport Management Courses in Chennai
Airport Ground Staff Training Courses in Chennai
Air hostess training in Bangalore
Aviation Academy in Chennai
Air Hostess Training in Chennai
Air Hostess Training in Chennai
Aviation Academy in Chennai
Aviation Courses in Bangalore

raneedevan さんのコメント...

nice blog..valuable information....thanks for sharing...
C C++ Training in Chennai
C Training in Chennai
C++ Training in Chennai
C C++ training in anna nagar
C C++ training in velachery
javascript training in chennai
core java training in chennai
Html5 Training in Chennai
DOT NET Training in Chennai
QTP Training in Chennai

shwethapriya さんのコメント...

Thanks for updating this information. Good job.
German Classes in Chennai
German Language Classes in Chennai
French Classes in Chennai
pearson vue test center in chennai
Informatica Training in Chennai
Data Analytics Courses in Chennai
German Classes in T Nagar
German Classes in Anna Nagar

yuvaprithika さんのコメント...

I would definitely thank the admin of this blog for sharing this information with us. Waiting for more updates from this blog admin.
Blue Prism Training in Chennai
Blue Prism Training
Azure Training in Chennai
Cloud Computing Training in Chennai
Automation Anywhere Training in Chennai
Blue Prism Training in Anna Nagar
Blue Prism Training in T Nagar
Blue Prism Training in OMR
Blue Prism Training in Porur

Aruna Ram さんのコメント...


Lovely post! This is a very fruitful post and I am impressed with the effort you have so obviously put into this content. Thanks for sharing.

Oracle Training in Chennai
best oracle training institute in chennai
Corporate Training in Chennai
Embedded System Course Chennai
Unix Training in Chennai
Pega Training in Chennai
Excel Training in Chennai
Soft SkillsTraining in Chennai
JMeter Training in Chennai
Oracle Training in Anna Nagar

navidevan さんのコメント...

Nice blog.....thanks for sharing...
core java training in chennai
core java training institutes in chennai
Best core java Training in Chennai
core java training in anna nagar
core java training in vadapalani
C C++ Training in Chennai
javascript training in chennai
Hibernate Training in Chennai
LoadRunner Training in Chennai
Mobile Testing Training in Chennai

Riya Raj さんのコメント...

The way you have conveyed your blog is more impressive.... good blog...
JAVA Training in Chennai
JAVA Course in Chennai
Java Training
Java classes in chennai
JAVA Training in Annanagar
java training in vadapalani
Digital Marketing Course in Chennai
Python Training in Chennai
Big data training in chennai
Selenium Training in Chennai

Sadhana Rathore さんのコメント...

I gathered lots of information from your blog and it helped me a lot. Keep posting more.
ccna course in Chennai
ccna Training in Chennai
ccna Training institute in Chennai
AngularJS Training in Chennai
Ethical Hacking course in Chennai
PHP Training in Chennai
CCNA course in Thiruvanmiyur
CCNA course in Porur
CCNA course in Adyar

manjuragul さんのコメント...

I really enjoyed this article. I need more information to learn so kindly update it.
DOT NET Training in anna nagar
DOT NET Training in Chennai
DOT NET Training in OMR
Software testing training in T Nagar
hadoop training in T Nagar
DOT NET Training in T nagar
SEO Training in OMR
Digital Marketing Course in anna nagar

Reshma さんのコメント...


Your post is really awesome .It is very helpful for me to develop my skills.
SEO Training in Velachery
SEO Training in T Nagar
SEO Training in Tambaram
SEO Training in Anna nagar
SEO Training in Porur
SEO Training in Thiruvanmiyur
SEO Training in Adyar
SEO Training in OMR
SEO Training in Vadapalani

Anbarasan14 さんのコメント...

Nice post. Thanks for sharing this post with us.
Spoken English Classes in Coimbatore
Best Spoken English Classes in Coimbatore
Spoken English Class in Coimbatore
Spoken English in Coimbatore
Spoken English Classes in Chennai
IELTS Coaching in Chennai
English Speaking Classes in Mumbai
IELTS Classes in Mumbai
Spoken English Class in Anna Nagar
IELTS Coaching in Tambaram

Patell Priya さんのコメント...

Nice Blog...Thanks for sharing the article waiting for the next update...
Artificial Intelligence Course in Chennai
best artificial intelligence training in chennai
C C++ Training in Chennai
javascript training in chennaiHtml5 Training in Chennai
QTP Training in Chennai
Spring Training in Chennai
DOT NET Training in Chennai
clinical sas training in chennai

Riya Raj さんのコメント...

Fantastic blog!!! Thanks for sharing with us, Waiting for your upcoming data.
Digital Marketing Course in Chennai
Digital Marketing Course
best digital marketing course in chennai
Digital Marketing Training in Chennai
Digital marketing course in vadapalani
Digital marketing course in porur
Python Training in Chennai
Hadoop Training in Chennai
Big data training in chennai
JAVA Training in Chennai

shivam さんのコメント...

Flying Shift - Packers & Movers in Bhopal