简体   繁体   中英

How can I most efficiently execute this recursive/iterative CPU intensive android task?

Some Background Info: I have made a program that given an arraylist of letters, and an array of integers finds all the combinations of words that can exist inside this arraylist where the words length is one of the integers in the int array (wordSizes). ie given h, o, p, n, c, i, e, t, k and the integers 5 and 4, the solution would be: phone tick.

My problem right now: Inputs usually are about 25 characters and the output should usually return 5 word combinations. I originally made this a console application for dekstop, and runtimes are generally less than 1 minute. I decided to port it to android and runtimes reach over 35 minutes. I am quite a beginner and not sure about how to run a CPU intensive task on Android.

public void findWordsLimited(ArrayList<Character> letters) {

    for (String s1 : first2s) {
        for (String s2 : possibleSeconds) {

            boolean t = true;

            String s1s2 = s1.concat(s2);
            ArrayList<Character> tempLetters = new ArrayList<Character>(letters);
            for (int i = 0; i < s1s2.length(); i++) {
                if (tempLetters.contains(s1s2.charAt(i)))
                    tempLetters.remove(Character.valueOf(s1s2.charAt(i)));
                else
                    t = false;
            }

            if (t) {
                helperFindWordsL(tempLetters, s1 + " " + s2, 2);
            }
        }
    }

}

public void helperFindWordsL(ArrayList<Character> letters, String prefix , int index) {

    boolean r;

    if (letters.size() <= 1) {
        output += "Success : " + prefix + "\n";
        Log.i(TAG, prefix);
    }

    else if (index < wordSizes.size()){

        for (String s : lastCheck) {

            if (s.length() == wordSizes.get(index)) {

                ArrayList<Character> templetters = new ArrayList<Character>(letters);
                r = true;

                for (int j = 0; j < s.length(); j++) {
                    if (templetters.contains(s.charAt(j)))
                        templetters.remove(Character.valueOf(s.charAt(j)));
                    else {
                        r = false;
                        templetters = new ArrayList<Character>(letters);
                    }
                }

                if (r)
                    helperFindWordsL(templetters, prefix + " " + s, index + 1);
            }
        }
    }

}

I am not too concerned about the algorithm, as this might be confusing because it is part of a bigger project to solve a word game puzzle. A few questions:

How would I get a CPU intensive task like this finished fastest? Right now I call the method findWordsLimited() from my MainActivity. On my desktop app (where it says output += Success... in HelperFindWordsL) I would print all solutions to the console, but right now I have made it so that the method adds to and in the end returns a giant string (String output) back to the MainActivity, with all solutions and that String is put inside of a TextView. Is that an inefficient way to display the data? If so, could you please help explain a better way?

Should I be running this as a backgroud/foreground process or thread instead of just calling it from the MainActivity?

How can i get runtimes on my android that are currently 20x slower than my desktop faster?

Try to replace recursion with cycles, and use arrays instead of lists, to avoid inserts etc, direct access to array members is much faster. Pay main attention to the most inner loop which uses templetters.contains(s.charAt(j)) , optimization of this part of code will give main effect.

You may add break; after t = false;

String s1s2 = s1.concat(s2); - it's not good to create a new String object for such case - it makes unnecessary work for GC . I would replace it with 2 cycles through s1 then s2

You could use 'letters' instead of ArrayList<Character> tempLetters = new ArrayList<Character>(letters); , just marking some items there as deleted. No need to create local clones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM