1
\$\begingroup\$

How can I use a CyclicBarrier with N-THREAD according to my core for optimize this code?

My code has an input like this:

i 3
a duna automobile deserto -1 
a nissan auto automobile -1 
s aut -1

i 2
a pesca sport frutta -1
s sport -1

END
  • i = 3 means that there are 3 lines.
  • "a" means that I'm adding an element (first occurrence after "a") and the other words after the element are my "tags".
  • "s" means that I have to find the tags that have as substring the word "aut" for the first block and "sport" for the second block.

At the end the output is like this:

2

1
  • "2" means that I have two elements "duna" & "nissan" that have tags (auto, automobile, auto) that start with the word "aut".
  • "1" means that I have the element "pesca" that has tag (sport) that starts with the sub-tag "sport".

With END, the program is finished.

package Prove;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class FinalMap {


    public static void main(String args[]) throws IOException {


        HashMap<String, Set<String>> map = new HashMap<String, Set<String>>();
        java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in);
        java.io.BufferedReader stdin = new java.io.BufferedReader(isr, 16 * 1024);

        String line = stdin.readLine();
        final String ADD_COMMAND = "a";
        final String ITEM_COMMAND = "i";
        final String SEARCH_COMMAND = "s";
        final String END_MARKER = "<END>";


        while (!line.equals("<END>")) {

            String elemento = "" ;


            if (line.startsWith(ITEM_COMMAND)) 
            {
                map = new HashMap<String, Set<String>>();
            }



            else if (line.startsWith(ADD_COMMAND)) {

                String[] parts = line.split("\\s+");

                for (int i = 2; i < parts.length - 1; ++i) {
                    String element = parts[1];
                    String tag = parts[i];
                    for (int j = 1; j <= parts[i].length(); ++j) {
                        String tagPrefix = tag.substring(0, j);
                        Set<String> elements = map.get(tagPrefix);
                        if (elements == null) {
                            elements = new HashSet<String>();
                            map.put(tagPrefix, elements);
                        }
                        elements.add(element);
                    }
                }


            }


            else if (line.startsWith(SEARCH_COMMAND)) {
                String subtag = line.substring(2,line.length()-3);
                if (map.containsKey(subtag)) {
                    System.out.println(map.get(subtag).size());
                } else {
                    System.out.println("missing");
                }
            }
           line = stdin.readLine();
        }
        stdin.close();
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. \$\endgroup\$ – Simon Forsberg Sep 9 '14 at 10:12
  • 1
    \$\begingroup\$ Please see our guidelines for asking a follow-up question. Your question seems to be a follow-up to this one \$\endgroup\$ – Simon Forsberg Sep 9 '14 at 10:20
  • \$\begingroup\$ I saw.This is a follow-up question because I changed the code and I would to know how to optimize more it \$\endgroup\$ – user3572461 Sep 9 '14 at 10:28
  • \$\begingroup\$ You can always go back to the previous question and look again: The opening and closing braces are still not used consistently, there's a lot of spacing and I'm not sure why. You also have a variable called END_MARKER yet you don't use it in your while loop. Seems you haven't fully addressed all the issues that they gave feedback on. \$\endgroup\$ – ZeroStatic Sep 9 '14 at 10:39
2
\$\begingroup\$
        String elemento = "" ;

This variable is unused.

Additionally I'd clean up the blank lines you have, it's dangerous to have an if statement end, and then a couple blank lines later, there's an else that continues the if.

            for (int i = 2; i < parts.length - 1; ++i) {
                String element = parts[1];
                String tag = parts[i];
                for (int j = 1; j <= parts[i].length(); ++j) {
                    String tagPrefix = tag.substring(0, j);
                    Set<String> elements = map.get(tagPrefix);
                    if (elements == null) {
                        elements = new HashSet<String>();
                        map.put(tagPrefix, elements);
                    }
                    elements.add(element);
                }
            }

There's a performance optimization here; parts[i].length() is not free. Use final int tagLength = parts[i].length(), then test against that in your for-loop condition.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.