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

  • Home
  • SEARCH
  • 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 6746129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:17:12+00:00 2026-05-26T12:17:12+00:00

Im doing a frequency dictionary, in which i read 1000 files, each one with

  • 0

Im doing a frequency dictionary, in which i read 1000 files, each one with about 1000 lines. The approach i’m following is:

  • BufferedReader to read fileByFile
  • read the first file, get the first sentence, split the sentence to an array string, then fill in an hashmap with the values from the string array.
  • do this for all the senteces in that file
  • do this for all 1000 files

My problem is, this is not a very efficient way to do it, i’m taking about 4 minutes to do all this. I’v increased heap size, refactored the code to make sure i’m not doind something wrong. For this approach, i’m completly sure there’s nothing i can improve in the code.

My bet is, each time a sentece is read, a split is applied, which, multiplied by 1000 sentences in a file and by 1000 files is a huge ammount of splits to process.
My idea is, instead of read and process file-by-file, i could read each file to a char array, and then make the split only once per file. That would ease the ammount of processing times consuming with the split. Any suggestions of implementation would be appreciated.

  • 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-26T12:17:12+00:00Added an answer on May 26, 2026 at 12:17 pm

    OK, I have just implemented the POC of your dictionary. Fast and dirty. My files contained 868 lines each one but I created 1024 copies of the same file. (This is table of contents of Spring Framework documentation.)

    I ran my test and it took 14020 ms (14 seconds!). BTW I ran it from eclipse that could decrease the speed a little bit.

    So, I do not know where your problem is. Please try my code on your machine and if it runs faster try to compare it with your code and understand where the root problem.

    Anyway my code is not the fastest I can write.
    I can create Pattern before loop and the use it instead of String.split(). String.split() calls Pattern.compile() every time. Creating pattern is very expensive.

    Here is the code:

    public static void main(String[] args) throws IOException {
        Map<String, Integer> words = new HashMap<String, Integer>();
    
        long before = System.currentTimeMillis();
    
        File dir = new File("c:/temp/files");
        for (File file : dir.listFiles()) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            for (String line = reader.readLine();  line != null;  line = reader.readLine()) {
                String[] lineWords = line.split("\\s+");
                for (String word : lineWords) {
                    int count = 1;
                    Integer currentCount = words.get(word);
                    if (currentCount != null) {
                        count = currentCount + 1;
                    }
                    words.put(word, count);
                }
            }
        }
    
        long after = System.currentTimeMillis();
    
        System.out.println("run took " + (after - before) + " ms");
        System.out.println(words);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a project in Java in which I'm looking at the frequency characters
I'm a non-computer science student doing a history thesis that involves determining the frequency
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Doing my first SL4 MVVM RIA based application and i ran into the following
The frequency with which I am coming across the situation where I have to
Doing some initial research on a new project and one of my requirements is
I have a Dictionary containing 10 keys, each with a list containing up to
AM doing a letter frequency analyzer program just like this website http://www.characterfrequencyanalyzer.com/ , but
I'm doing a data migration at the moment and one task I'm doing is
I am doing research about dedicated I/O software that would run on consumer hardware.

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.