1

I'm using the google translate api to translate a list of strings, in this case - candies and their respective colors. I do not have a problem translating, however, the processing time is very slow - somewhere around 20 seconds. I am looking to improve the speed of this. Any suggestions are appreciated, thanks. My code is below:

This is from the service layer of my Spring application. After processing, I pass a List<String> to my controller layer in my REST service.

The dataset I am processing is about 100 elements.

public List<String> getSortedCandies() {
    List<Candy> candyList = new ArrayList<>(this.candyList);

    //sort candies
    candyList = candyList.stream()
            .sorted((o1, o2) -> {
                int nameResult = o1.getName().toUpperCase().compareTo(o2.getName().toUpperCase());
                if (nameResult == 0) {
                    return o1.getColor().toUpperCase().compareTo(o2.getColor().toUpperCase());
                }
                return nameResult;
            }).collect(Collectors.toList());

    Translate translate = TranslateOptions.getDefaultInstance().getService();

    List<String> translatedList = new ArrayList<>();

    //translate from english to spanish
    for (Candy candy : candyList) {
        Detection detection = translate.detect(candy.toString());
        String detectedLanguage = detection.getLanguage();
        Translation translation = translate.translate(
                candy.toString(),
                Translate.TranslateOption.sourceLanguage(detectedLanguage),
                Translate.TranslateOption.targetLanguage("es")
        );
        translatedList.add(translation.getTranslatedText());
    }

    return translatedList;
}
Joe Berg
  • 774
  • 2
  • 8
  • 21

1 Answers1

0

You can modify your code to use parallelStream() and Collections.synchronizedList():

public class StackOverflowSample {

    private List<Candy> candyList = new ArrayList<>();

    public StackOverflowSample() {

    }

    public StackOverflowSample(List<Candy> candyList) {
        this.candyList = candyList;
    }

    public List<String> getSortedCandies() {

        // sort candies
        candyList = candyList.stream().sorted((o1, o2) -> {
            int nameResult = o1.getName().toUpperCase().compareTo(o2.getName().toUpperCase());
            if (nameResult == 0) {
                return o1.getColor().toUpperCase().compareTo(o2.getColor().toUpperCase());
            }
            return nameResult;
        }).collect(Collectors.toList());

        Translate translate = TranslateOptions.getDefaultInstance().getService();

        List<String> translatedList = Collections.synchronizedList(new LinkedList<>());

        candyList.parallelStream().forEach(c -> {
            Detection detection = translate.detect(c.toString());
            String detectedLanguage = detection.getLanguage();
            Translation translation = translate.translate(c.toString(),
                    Translate.TranslateOption.sourceLanguage(detectedLanguage),
                    Translate.TranslateOption.targetLanguage("es"));
            translatedList.add(translation.getTranslatedText());
        });

        return translatedList;
    }
}

I have tested the code but maybe you could compare by getting current time before and after the process as recommended in this answer:

https://stackoverflow.com/a/1770475/9015852

Victor M Perez
  • 2,185
  • 3
  • 19
  • 22