SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
You?Andraz Poje wrote:Is anybody interested to write a test code to measure comparison speed?
OCUP UML fundamental and ITIL foundation
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Jan Cumps wrote:
You?Andraz Poje wrote:Is anybody interested to write a test code to measure comparison speed?
Rob Prime wrote: I think Henry posted how to do that a few weeks ago.
OCUP UML fundamental and ITIL foundation
Henry Wong wrote:
Rob Prime wrote: I think Henry posted how to do that a few weeks ago.
I probably did, but I don't mind doing it again...![]()
1. Get rid of everything that is not relevant. Don't do the "if" or the "System.out", as it is the same in all three cases, so why bother measuring it?
2. Be careful with short strings, as it is likely that you are measuring the setup more than the actual comparison. Also, don't make it obviously not equal, as there are probably short circuit code with a few of the options. Maybe measure two strings, where they start off the same, but are different later.
3. Do it in a loop. Maybe do the operation a million times. And measure the million times as a single number.
4. Make sure you have tons of memory, and do a gc() prior to taking the start time, just in case (to avoid taking a gc hit during the measurement).
5. Run it many many times -- meaning do the three tests in a loop over and over. This way, you can ignore the first few runs, in order to discount the JIT. This will also allow you to discount outliers caused by the GC too.
Henry
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Henry Wong wrote:
And ... you didn't do my recommendations number 4 and 5... which tries to discount the JIT and GC.
Henry
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Henry Wong wrote:
And ... you didn't do my recommendations number 4 and 5... which tries to discount the JIT and GC.
Henry
OCUP UML fundamental and ITIL foundation
Mike Thon wrote:Why not use == for comparing strings?
Andraz Poje wrote:
How do I get more precise result?
Istvan Kovacs wrote:
Andraz Poje wrote:
How do I get more precise result?
Besides running many times and making sure you get the JIT compiler to actually compile your code before benchmarking, you may want to check out System.nanoTime().
R van Vliet wrote:Are there any VM implementations where System.nanoTime() isn't System.currentMillis() * 1000? I have yet to run into any.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |