0

This program is to generate all possible strings from given char sequence with given set but it take much time, how to reduce memory usage and speed up this prodram to work in multi task with out generate the same string twice??

public class LapTest {

static int q=0;

public static void main(String[] args) {
  System.out.println("First Test");
    char set1[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    int k = 4;
    printAllKLength(set1, k);

      //Do some logic
}    

// The method that prints all possible strings of length k.  It is
//  mainly a wrapper over recursive function printAllKLengthRec()
static void printAllKLength(char set[], int k) {
    int n = set.length;        
    printAllKLengthRec(set, "", n, k);
}

// The main recursive method to print all possible strings of length k
static void printAllKLengthRec(char set[], String prefix, int n, int k) {

    // Base case: k is 0, print prefix
    if (k == 0) { q++;
        System.out.println(prefix);

        if (prefix.equals("hkka")) {
           System.exit(0);
        }
        return;
    }

    // One by one add all characters from set and recursively 
    // call for k equals to k-1
    for (int i = 0; i < n; ++i) {

        // Next character of input added
        String newPrefix = prefix + set[i]; 

        // k is decreased, because we have added a new character
        printAllKLengthRec(set, newPrefix, n, k - 1); 
    }
}
}
3
  • Consider using a ExecutorService. There is a tutorial on concurrency you can work through. Commented Nov 10, 2016 at 19:44
  • Multicore will not help much. Better algorithms will. Try attaching visualvm to see how much time is spent by concatenating strings. Commented Nov 10, 2016 at 19:47
  • 1
    Better algorithm will not help. suppose I want to generate a 7 length string then it will be 8031810176 different string :D Commented Nov 10, 2016 at 19:52

2 Answers 2

2

Your program is very likely I/O bound, this means writing the output to the screen will take more time than calculating the string. So processing in parallel will not help you in any way. No matter what you do with the string later on, on a modern computer, the sink (consumer of your string) is very likely slower than calculating it, even with an imperfect solution.

Also for writing the string to screen, use a buffered writer which will make your program much faster. Define a BufferedOutputStream in your class:

static BufferedOutputStream out = new BufferedOutputStream(System.out, 81920); 

Then write with out.write((prefix+"\n").getBytes()); and don't forget to flush the stream with out.flush(); on both the end of the program and on the System.exit(0);. This will give you a significant performance improvement.

Also try to not solve the problem without recursion, that might give you another speed pump.

Sign up to request clarification or add additional context in comments.

1 Comment

I will not print it in my final program . i am only print it to sure it is work
0

There are many performance improvements you should make before you try to make it multi-threaded. I can see several obvious things you could do to improve performance. But experience has shown that most coders (myself included) are terrible at guessing where code should be tweaked to make it more efficient.

So my advice is to learn to use a profiler. Profile your code and find out where it is spending cpu cycles then tweak the code in the right places. You will get much better results learning to do that. Good modern IDEs make this a pretty simple process for simple programmes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.