r/javahelp 8d ago

Is the TreeSet Slow or is My Benchmark Wrong?

Currently to Optimize my library I have been reading ben paff guide on iterative approach of tree, from which I got the Idea of RBT and AVL for iterative approach. In which I made use AVL as parent based tree. But it is that when I did the benchmark with JVM21 these were my result ,it's tested three time for consistent behaviour of data, error and changes to occur for each time. which stands out firmly consistent behavior. tthis is one sample of it.

Benchmark                                                         (size)    Mode      Cnt        Score   Error   Units
InsertDeleteBenchmark.avlInsertDelete                              10000  sample  2828056      228.686 ± 2.651   ns/op
InsertDeleteBenchmark.bstInsertDelete                              10000  sample  3131645      190.531 ± 2.344   ns/op
InsertDeleteBenchmark.javaTreeSetInsertDelete                      10000  sample  4416191      228.719 ± 1.637   ns/op
InsertDeleteBenchmark.rbtInsertDelete                              10000  sample  2766974      211.846 ± 2.505   ns/op
InsertDeleteBenchmark.treapInsertDelete                            10000  sample  4749890      232.499 ± 1.485   ns/op

The benchmark was done 3 time, the config I used were for the benchmark were
10K - randomized dataset
warmup = 3 warmup time = 5s each
measurement = 3 measurement time = 5s each
fork = 5.
All the tree used same Set as interface for test. The doubt is currently my RBT tree is working safe with recursive delete and performing edge with TreeSet. What is the reason for TreeSet to be slow given that TreeSet is highly optimized. When I think of Benchmark I assume to be correct. This is really too good to be proud or confused??. But there is greed of also stripping recursive delete of RBT which I want to do and not do. Because it's too hard😢
For reference of Benchmark code

Edit:My wordings and grammar are not too good please do ignore that.

4 Upvotes

3 comments sorted by

u/AutoModerator 8d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/idontlikegudeg 8d ago

Your benchmark consists of adding, then removing the same key just added. This is a rather exotic thing to do and IMHO has very little practical relevance. If this is really meant to test insert and delete operations and it is your only test (instead of a very test special case), I’d call the benchmark flawed.

1

u/Chaos-vy17 8d ago

Yes, It seems that inserting and deleting the same key was actually still in hot cache so it does have a flaw. I will work on it.