Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  calling methods using reflection  (Read 2387 times)
0 Members and 1 Guest are viewing this topic.
Offline thijs

Junior Member




Lava games rock!


« Posted 2006-01-16 22:10:00 »

Im wondering what kind of degrade in performance I should expect when I call methods using reflection (using an Method[]) compared to calling methods on objects the regular way.

Im not too familiar with how current (and old) JVM's handle this kind of reflection, is there even a difference when I cache my to be called methods in this array upon initialisation of my app?

Thanks,

Thijs

<a href="http://www.dzzd.net">3DzzD!</a>
<a href="http://www.arcazoid.com">Arcazoid!</a>
Online princec

JGO Kernel


Medals: 369
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2006-01-16 22:46:55 »

It's pretty fast, much faster than it used to be, but nowhere near as fast as the JITted code Hotspot produces for method calls. Run a little benchmark to find out.

Cas Smiley

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #2 - Posted 2006-01-16 22:55:00 »

Are you sure you really need this?

Casting over to some interface is good enough in most cases.

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #3 - Posted 2006-01-16 23:38:08 »

If you need it, calling through a Method is not too bad.

Lookign that method up costs significantly more so cache Method references if at all possible.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Linuxhippy

Senior Member


Medals: 1


Java games rock!


« Reply #4 - Posted 2006-01-17 07:12:39 »

btw. reflection was improved in 1.4, Sun's jvms prior 1.4 may show poor reflection performance.
But in general its pretty fast if you cache the method-objects.

lg Clemens
Offline thijs

Junior Member




Lava games rock!


« Reply #5 - Posted 2006-01-23 10:50:18 »

Quote
It's pretty fast, much faster than it used to be, but nowhere near as fast as the JITted code Hotspot produces for method calls. Run a little benchmark to find out.

Yeah I know, im lazy  Wink
I'll dig up a profiler to do some tetss...

Quote
Are you sure you really need this?
Casting over to some interface is good enough in most cases.

Unfortunatly I need it in this case... I need to be able to call getters / setters for attributes marked as transient without knowing anything about the class structure. I crawl through the specified class and for each transient field I lookup the getter / setter and reference them in a Method[].

<a href="http://www.dzzd.net">3DzzD!</a>
<a href="http://www.arcazoid.com">Arcazoid!</a>
Offline V

Senior Newbie





« Reply #6 - Posted 2006-01-29 07:27:58 »

if I cache the methods in a HashMap<String, Method>, it's 40% slower than using getMethod, so sticking with getMethod is definitely the better solution.

but, during my tests, this line
                    getClass().getMethod("__" + input.sval, new Class[] { HashMap.class }).invoke(this, new Object[] { data } );
several times actually performed better than this line, even if only by a small margin (10%)
                    ((AbstractInstance) caller).__out(data);

huh, wacky.
Offline Linuxhippy

Senior Member


Medals: 1


Java games rock!


« Reply #7 - Posted 2006-01-29 20:36:47 »

if I cache the methods in a HashMap<String, Method>, it's 40% slower than using getMethod, so sticking with getMethod is definitely the better solution.

This is hardly possible, maybe you've some other effects? microbenchmarks?
Since getMethod would do in the best case exactly the same, just look into a HashMap wether the method-object has already looked up - however since its thread-safe there would be even locking-overheads (even if uncontended), there is almost no chance for getMethod beeing faster.

lg Clemens
Offline Jeff

JGO Coder




Got any cats?


« Reply #8 - Posted 2006-01-29 22:38:22 »

LH is right that makes no sense.

I'd suggest you profile and see what is really going on,

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline V

Senior Newbie





« Reply #9 - Posted 2006-01-30 02:38:18 »

funny, it's now indeed a good bit faster when cached. I could have sworn it wasn't earlier Huh
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #10 - Posted 2006-01-30 02:43:17 »

well if your caching logic was messed up such that you always had a cache miss, that would make it take longer then assuming the miss all the time which is what not caching at all is equivalent to.

Thats my "most likely scenario."

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline V

Senior Newbie





« Reply #11 - Posted 2006-01-30 14:33:54 »

may be. oh well, works fine & fast now Wink
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (39 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (36 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!