• Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance of Value Object Vs HashMap  RSS feed

 
R.Lakshmi Narasimhan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I just happened to write a code to compare the performance in terms of execution time of a value object with getters and setters against a hashmap. The value object has 150 string fields with just setters and getters. I had written code to populate the value object using setters with some string and then in hashmap using put method with 150 strings. Then got the values from value object using getters and then got value from hashmap using get method. To calculate the time taken I have compared System.nanoTime() for these scenarios.
Program is as:



Result after execution in my machine
Time to ins BO 612807
Time to ins Map 95017
Time1 to populate BO 6028418
Time2 to populate Map 1081599
Time3 fetch from BO 4636277
Time4 fetch from Map 4263164

I have repeated the execution many times and the result is almost similar.
As you see in the result HashMap based times are better in object creation and setting the values in the object than value object. Am I missing something in this program?
I don't understand why HashMap is better in performance than value object. As far as I know both these approaches are going to call a method and the hashmap needs to do extra hashing to find the bucket but value object class does not have things to do.
Is there any handling for hashmap or collections in JVM level or are these objects handled differently? Is there any explanation for this?
 
Frank Pavageau
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your benchmark is flawed, you can't just execute different tests in the same run, as the JIT is affected by the accumulation of profiles. There's no warmup either, or repetition to get a significant sample size. Given enough time, there would also be some dead code elimination probably going on, as the computations are not used for anything in the end.

Use JMH (after reading the samples to understand what you should pay attention to) to bench the various options, and publish your code and results so other people can weigh in on the correctness.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!