Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 916999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:08:22+00:00 2026-05-15T18:08:22+00:00

I have a program which fetches records from database (using Hibernate) and fills them

  • 0

I have a program which fetches records from database (using Hibernate) and fills them in a Vector. There was an issue regarding the performance of the operation and I did a test with the Vector replaced by a HashSet. With 300000 records, the speed gain is immense – 45 mins to 2 mins!

So my question is, what is causing this huge difference? Is it just the point that all methods in Vector are synchronized or the point that internally Vector uses an array whereas HashSet does not? Or something else?

The code is running in a single thread.

EDIT:
The code is only inserting the values in the Vector (and in the other case, HashSet).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:08:22+00:00Added an answer on May 15, 2026 at 6:08 pm

    If it’s trying to use the Vector as a set, and checking for the existence of a record before adding it, then filling the vector becomes an O(n^2) operation, compared with O(n) for HashSet. It would also become an O(n^2) operation if you insert each element at the start of the vector instead of at the end.

    If you’re just using collection.add(item) then I wouldn’t expect to see that sort of difference – synchronization isn’t that slow.

    If you can try to test it with different numbers of records, you could see how each version grows as n increases – that would make it easier to work out what’s going on.

    EDIT: If you’re just using Vector.add then it sounds like something else could be going on – e.g. your database was behaving differently between your different test runs. Here’s a little test application:

    import java.util.*;
    
    public class Test {
      public static void main(String[] args) {
        long start = System.currentTimeMillis();
        Vector<String> vector = new Vector<String>();
        for (int i = 0; i < 300000; i++) {
          vector.add("dummy value");
        }
        long end = System.currentTimeMillis();
        System.out.println("Time taken: " + (end - start) + "ms");
      }
    }
    

    Output:

    Time taken: 38ms

    Now obviously this isn’t going to be very accurate – System.currentTimeMillis isn’t the best way of getting accurate timing – but it’s clearly not taking 45 minutes. In other words, you should look elsewhere for the problem, if you really are just calling Vector.add(item).

    Now, changing the code above to use

    vector.add(0, "dummy value"); // Insert item at the beginning
    

    makes an enormous difference – it takes 42 seconds instead of 38ms. That’s clearly a lot worse – but it’s still a long way from being 45 minutes – and I doubt that my desktop is 60 times as fast as yours.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have this program which fetches orders from a database. My problem is
I have a program which accepts 2 N -digit numbers, multiplies them using threads
I have a sample application which loads 2 records ro database and then fetches
I have program which writes to database which folders are full or empty. Now
i have program which prints all char from char_min to char_max here is code
I have a program which write logging as Text File. namespace logging { using
I have a program which transmits(broadcasts) and receives UDP packets simultaneously using pthreads. What
I have socket program which sends a file from socket client to socket server
I have program which reads MSMQ using GetAllMessages but it does not remove messages
I have a program which creates a timer using timerfd_create (the timer when it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.